<?php
/**
 *  * 系统-受国家计算机软件著作权保护 - !
 * =========================================================
 * Copy right 2018-2025 成都海之心科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: http://www.ohyu.cn
 * 这不是一个自由软件！在未得到官方有效许可的前提下禁止对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 * User: ohyueo
 * Date: 2023/7/31
 * Time: 20:18
 */

 namespace app\admin\controller;
use think\facade\Db;
use think\facade\Log;
use think\facade\View;
use app\admin\traits\AdminAuth;

class Autocurd extends common
{
    use AdminAuth;

    //定义一个数据库前缀
    protected $prefix = 'escort_';
    //定义一个模型前缀
    protected $modelprefix = 'Escort';
    /**生成文件**/
    public function codetopnav(){
        $data['status']=0;
        $data['msg']='生成失败';
        $id=input('param.id/d');//顶级权限id
        if($id){
            $topx=Db::name('escort_admin_permission')->where('id',$id)
                ->field(['name','icon'])->find();
            $toptit=$topx['name'];
            $icon=$topx['icon'];
            //查询顶级id是这个id的  所有 权限  然后批量生成
            $list=Db::name('escort_admin_permission')->where('parent_id',$id)
                //->where('id','>','80')
                ->select()->toArray();
            if($list){
                for($i=1;$i<count($list);$i++){
                    $model=$list[$i]['controller'];
                    $action=$list[$i]['action'];
                    $title=$list[$i]['name'];
                    $table='escort_'.$model.'_'.$action;
                    //兼容老系统 测试用
//                    if($i==0){
//                        $table='escort_zonghe_img';
//                    }else if($i==1){
//                        $table='escort_noti_list';
//                    }else if($i==2){
//                        $table='escort_web_text';
//                    }
                    $table=strtolower($table);
                    $model='Mer'.strtolower($model);
                    //查询数组里面的 所有字段
                    $cuid=Db::name('ohyueo_develo_curdlist')->where('table',$table)->value('id');
                    $reslist=Db::name('ohyueo_develo_curdtable')->where('listid',$cuid)->select()->toArray();
                    if($reslist){
                        //先存入当前这个顶级权限  查询是否存在
                        $topnav=Db::name('escort_addons_merchants_permission')->where('controller',$model)->where('parent_id',0)->find();
                        if(!$topnav){
                            //不存在则存入
                            $arrx=array(
                                'name' => $toptit,
                                'controller' => $model,
                                'action' => '',
                                'parent_id' => 0,
                                'is_nav'=>1,
                                'icon' => $icon,
                                'p_id' => 3
                            );
                            $topid=Db::name('escort_addons_merchants_permission')->insertGetId($arrx);
                        }else{
                            $topid=$topnav['id'];
                        }
                        //先存入权限信息
                        $ro=Db::name('escort_addons_merchants_permission')->where('parent_id',$topid)->where('controller',$model)->where('action',$action)->find();
                        if(!$ro){
                            $roarr=array(
                                'name' => $title,
                                'controller' => $model,
                                'action' => $action,
                                'parent_id' => $topid,
                                'is_nav'=>1,
                                'icon' => '',
                                'p_id' => 0
                            );
                            $rid=Db::name('escort_addons_merchants_permission')->insertGetId($roarr);
                            //添加  修改  删除
                            $roarr=array(
                                'name' => '添加',
                                'controller' => $model.'/'.$action,
                                'action' => 'add'.$action,
                                'parent_id' => $rid,
                                'is_nav'=>0,
                                'icon' => '',
                                'p_id' => 0
                            );
                            Db::name('escort_addons_merchants_permission')->insert($roarr);
                            //修改
                            $roarr=array(
                                'name' => '修改',
                                'controller' => $model.'/'.$action,
                                'action' => 'edit'.$action,
                                'parent_id' => $rid,
                                'is_nav'=>0,
                                'icon' => '',
                                'p_id' => 0
                            );
                            Db::name('escort_addons_merchants_permission')->insert($roarr);
                            //删除
                            $roarr=array(
                                'name' => '删除',
                                'controller' => $model.'/'.$action,
                                'action' => 'del'.$action,
                                'parent_id' => $rid,
                                'is_nav'=>0,
                                'icon' => '',
                                'p_id' => 0
                            );
                            Db::name('escort_addons_merchants_permission')->insert($roarr);
                        }
                        //循环数据
                        for($r=0;$r<count($reslist);$r++){
                            $reslist[$r]['title']=$reslist[$r]['name'];
                            $reslist[$r]['name']=$reslist[$r]['beizhu'];
                        }
                        //生成php文件
                        $this->modelcode($model,$action,$reslist,$title,$table);
                        $data['status']=1;
                        $data['msg']='生成成功';
                    }
                }
            }
        }
        return json($data);exit;
    }
    protected function modelcode($model,$action,$pararr,$title,$table){
        //判断是否有 此文件 php文件
        $path=app()->getRootPath().'/addons/merchants/controller/'.$model.'.php';
        if(!file_exists($path)) { //没有此文件 则生成
            $html="<?php \n/**\n*  * 系统-受国家计算机软件著作权保护 - !\n* =========================================================\n* Copy right 2018-2025 成都海之心科技有限公司, 保留所有权利。\n* ----------------------------------------------\n* 官方网址: http://www.ohyu.cn\n* 这不是一个自由软件！在未得到官方有效许可的前提下禁止对程序代码进行修改和使用。\n* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。\n* =========================================================\n* User: ohyueo\n* Date: ".(date('Y/m/d'))."\n* Time: ".(date('H:i'))."\n*/\nnamespace addons\merchants\controller;\nuse think\\facade\Log;\nuse think\\facade\Db;\nuse think\\facade\\Request;
            \nclass ".$model." extends Mercommon{\n\n ";

            $biao1="\t";$biao2="\t\t";$biao3="\t\t\t";$biao4="\t\t\t\t";
            //生成列表文件
            $html.=$biao1."public function ".$action."(){\n ";
            $html.=$biao2."\$merid = \$this->getmerid();\n\t\tif(!\$merid){\n\t\t\t\$data['msg'] = '数据异常';\n\t\t\treturn json(\$data);\n\t\t\texit;\n\t\t}\n";
            $html.=$biao2."if(request()->isPost()){\n";
            $html.=$biao3."\$page=input('param.page');\n".$biao3."if(!\$page){\n".$biao4."\$page=1;\n".$biao3."}\n".$biao3."\$limit=input('param.limit');\n".$biao3."if(!\$limit){\n".$biao4."\$limit=10;//每页显示条数\n".$biao3."}\n";
            //搜索数据
            $html.=$biao3."\$where=[];\n\t\t\t\$where['merid'] = \$merid;\n";
            $html.=$biao3."\$id=input('param.id');\n".$biao3."if(\$id){\n".$biao4."\$where[]=['id','=',\$id];\n".$biao3."}\n";
            //查询产品
            $html.=$biao3."\$count = Db::name('".$table."')->where(\$where)->count();\n".$biao3."\$data=Db::name('".$table."')->where(\$where)->page(\$page,\$limit)->order('id desc')->select()->toArray();\n";
            //返回数据
            $html.=$biao3."\$res = [\"data\" => \$data,\"code\"=>0,'message'=>'请求成功','count'=>\$count];\n".$biao3."return json(\$res);\n";
            $html.=$biao2."}\n";
            //显示页面
            $html.=$biao2."\$name='".$action."';\n\t\t\$permis = \$this->getPermissions('".$model."/".$action."');\n\t\t\$actions = \$this->actions;\n\t\t\$assign=['data'=>\$actions,'permis'=>\$permis,'merid'=>\$merid];\n\t\thook('merhook', ['type'=>'view','name'=>\$name,'assign'=>\$assign]);\n";
            $html.=$biao1."}\n";

            //生成添加文件
            $html.=$biao1."public function add".$action."(){\n ";
            $html.=$biao2."\$merid = \$this->getmerid();\n\t\tif(!\$merid){\n\t\t\t\$data['msg'] = '数据异常';\n\t\t\treturn json(\$data);\n\t\t\texit;\n\t\t}\n";
            $html.=$biao2."if(request()->isPost()){\n";
            $html.=$biao3."\$data = array('status' => 0, 'msg' => '未知错误');\n";
            if($pararr){
                $arrtit=$biao3."\$arr=array(\n";
                for ($i=0;$i<count($pararr);$i++){
                    $fna=$pararr[$i]['title'];
                    $html.=$biao3.'$'.$fna."=input('param.".$fna."');\n";
                    $arrtit.=$biao4."'".$fna."'=>$".$fna.",\n";
                }
                $arrtit.=$biao4."'merid'=>\$merid,\n";
                $arrtit.=$biao3.");";
                $html.=$arrtit."\n";
            }
            $html.=$biao3."\$id=Db::name('".$table."')->insertGetId(\$arr);\n".$biao3."\$text = '添加了".$title."-id='.\$id;\n".$biao3."\$this->writeActionLog(\$text);\n".$biao3."\$data['status'] = 1;\n".$biao3."return json(\$data);exit;\n";
            $html.=$biao2."}\n";
            //显示页面
            $html.=$biao2."\$name='edit".$action."';\n\t\t\$assign=[];\n\t\thook('merhook', ['type'=>'view','name'=>\$name,'assign'=>\$assign]);\n";
            $html.=$biao1."}\n";

            //生成修改文件
            $html.=$biao1."public function edit".$action."(){\n ";
            $html.=$biao2."\$merid = \$this->getmerid();\n\t\tif(!\$merid){\n\t\t\t\$data['msg'] = '数据异常';\n\t\t\treturn json(\$data);\n\t\t\texit;\n\t\t}\n";
            $html.=$biao2."if(request()->isPost()){\n";
            $html.=$biao3."\$data = array('status' => 0, 'msg' => '未知错误');\n";
            if($pararr){
                $arrtit=$biao3."\$arr=array(\n";
                for ($i=0;$i<count($pararr);$i++){
                    $fna=$pararr[$i]['title'];
                    $html.=$biao3.'$'.$fna."=input('param.".$fna."');\n";
                    $arrtit.=$biao4."'".$fna."'=>$".$fna.",\n";
                }
                $arrtit.=$biao3.");";
                $html.=$arrtit."\n";
            }
            $html.=$biao3."\$id = input('param.id');\n".$biao3."if (\$id){\n";
            $html.=$biao4."Db::name('".$table."')->where('id',\$id)->where('merid',\$merid)->update(\$arr);\n".$biao4."\$text = '修改了".$title."-id='.\$id;\n".$biao4."\$this->writeActionLog(\$text);\n".$biao4."\$data['status'] = 1;\n".$biao4."return json(\$data);exit;\n";
            $html.=$biao3."}\n";
            $html.=$biao2."}\n";
            //显示页面
            $html.=$biao2."\$name='edit".$action."';\n\t\t\$assign=[];\n\t\thook('merhook', ['type'=>'view','name'=>\$name,'assign'=>\$assign]);\n";
            $html.=$biao1."}\n";

            //生成删除页面
            $html.=$biao1."public function del".$action."(){\n ";
            $html.=$biao2."\$merid = \$this->getmerid();\n\t\tif(!\$merid){\n\t\t\t\$data['msg'] = '数据异常';\n\t\t\treturn json(\$data);\n\t\t\texit;\n\t\t}\n";
            $html.=$biao2."\$data = array('status' => 0,'msg' => '未知错误');\n".$biao2."\$array=input(\"param.id\");\n".$biao2."if(!\$array){\n".$biao3."\$data['msg']='参数错误';return json(\$data);exit;\n".$biao2."}\n";
            $html.=$biao2."\$arr=explode(\",\",\$array);\n".$biao2."for(\$i=0;\$i<count(\$arr);\$i++){\n";
                
            $html.=$biao3."if(\$arr[\$i]){\n".$biao4."Db::name('".$table."')->where('merid',\$merid)->where('id',\$arr[\$i])->delete();\n".$biao4."\$text = '删除了".$title."id='.\$arr[\$i].'的数据';\n".$biao4."\$this->writeActionLog(\$text);\n".$biao3."}\n";
            $html.=$biao2."}\n".$biao2."\$data['status']=1;\n".$biao2."return json(\$data);\n";
            $html.=$biao1."}\n";

            $html.="\n} ";
            file_put_contents($path,$html);
        }else{ //如果已经有这个文件了  则直接在倒数第二行插入代码
            $html="";
            $biao1="\t";$biao2="\t\t";$biao3="\t\t\t";$biao4="\t\t\t\t";
            //生成列表文件
            $html.=$biao1."public function ".$action."(){\n ";
            $html.=$biao2."\$merid = \$this->getmerid();\n\t\tif(!\$merid){\n\t\t\t\$data['msg'] = '数据异常';\n\t\t\treturn json(\$data);\n\t\t\texit;\n\t\t}\n";
            $html.=$biao2."if(request()->isPost()){\n";
            $html.=$biao3."\$page=input('param.page');\n".$biao3."if(!\$page){\n".$biao4."\$page=1;\n".$biao3."}\n".$biao3."\$limit=input('param.limit');\n".$biao3."if(!\$limit){\n".$biao4."\$limit=10;//每页显示条数\n".$biao3."}\n";
            //搜索数据
            $html.=$biao3."\$where=[];\n\t\t\t\$where['merid'] = \$merid;\n";
            $html.=$biao3."\$id=input('param.id');\n".$biao3."if(\$id){\n".$biao4."\$where[]=['id','=',\$id];\n".$biao3."}\n";
            //查询产品
            $html.=$biao3."\$count = Db::name('".$table."')->where(\$where)->count();\n".$biao3."\$data=Db::name('".$table."')->where(\$where)->page(\$page,\$limit)->order('id desc')->select()->toArray();\n";
            //返回数据
            $html.=$biao3."\$res = [\"data\" => \$data,\"code\"=>0,'message'=>'请求成功','count'=>\$count];\n".$biao3."return json(\$res);\n";
            $html.=$biao2."}\n";
            //显示页面
            $html.=$biao2."\$name='".$action."';\n\t\t\$permis = \$this->getPermissions('".$model."/".$action."');\n\t\t\$actions = \$this->actions;\n\t\t\$assign=['data'=>\$actions,'permis'=>\$permis,'merid'=>\$merid];\n\t\thook('merhook', ['type'=>'view','name'=>\$name,'assign'=>\$assign]);\n";
            $html.=$biao1."}\n";

            //生成添加文件
            $html.=$biao1."public function add".$action."(){\n ";
            $html.=$biao2."\$merid = \$this->getmerid();\n\t\tif(!\$merid){\n\t\t\t\$data['msg'] = '数据异常';\n\t\t\treturn json(\$data);\n\t\t\texit;\n\t\t}\n";
            $html.=$biao2."if(request()->isPost()){\n";
            $html.=$biao3."\$data = array('status' => 0, 'msg' => '未知错误');\n";
            if($pararr){
                $arrtit=$biao3."\$arr=array(\n";
                for ($i=0;$i<count($pararr);$i++){
                    $fna=$pararr[$i]['title'];
                    $html.=$biao3.'$'.$fna."=input('param.".$fna."');\n";
                    $arrtit.=$biao4."'".$fna."'=>$".$fna.",\n";
                }
                $arrtit.=$biao4."'merid'=>\$merid,\n";
                $arrtit.=$biao3.");";
                $html.=$arrtit."\n";
            }
            $html.=$biao3."\$id=Db::name('".$table."')->insertGetId(\$arr);\n".$biao3."\$text = '添加了".$title."-id='.\$id;\n".$biao3."\$this->writeActionLog(\$text);\n".$biao3."\$data['status'] = 1;\n".$biao3."return json(\$data);exit;\n";
            $html.=$biao2."}\n";
            //显示页面
            $html.=$biao2."\$name='edit".$action."';\n\t\t\$assign=[];\n\t\thook('merhook', ['type'=>'view','name'=>\$name,'assign'=>\$assign]);\n";
            $html.=$biao1."}\n";

            //生成修改文件
            $html.=$biao1."public function edit".$action."(){\n ";
            $html.=$biao2."\$merid = \$this->getmerid();\n\t\tif(!\$merid){\n\t\t\t\$data['msg'] = '数据异常';\n\t\t\treturn json(\$data);\n\t\t\texit;\n\t\t}\n";
            $html.=$biao2."if(request()->isPost()){\n";
            $html.=$biao3."\$data = array('status' => 0, 'msg' => '未知错误');\n";
            if($pararr){
                $arrtit=$biao3."\$arr=array(\n";
                for ($i=0;$i<count($pararr);$i++){
                    $fna=$pararr[$i]['title'];
                    $html.=$biao3.'$'.$fna."=input('param.".$fna."');\n";
                    $arrtit.=$biao4."'".$fna."'=>$".$fna.",\n";
                }
                $arrtit.=$biao3.");";
                $html.=$arrtit."\n";
            }
            $html.=$biao3."\$id = input('param.id');\n".$biao3."if (\$id){\n";
            $html.=$biao4."Db::name('".$table."')->where('id',\$id)->where('merid',\$merid)->update(\$arr);\n".$biao4."\$text = '修改了".$title."-id='.\$id;\n".$biao4."\$this->writeActionLog(\$text);\n".$biao4."\$data['status'] = 1;\n".$biao4."return json(\$data);exit;\n";
            $html.=$biao3."}\n";
            $html.=$biao2."}\n";
            //显示页面
            $html.=$biao2."\$name='edit".$action."';\n\t\t\$assign=[];\n\t\thook('merhook', ['type'=>'view','name'=>\$name,'assign'=>\$assign]);\n";
            $html.=$biao1."}\n";

            //生成删除页面
            $html.=$biao1."public function del".$action."(){\n ";
            $html.=$biao2."\$merid = \$this->getmerid();\n\t\tif(!\$merid){\n\t\t\t\$data['msg'] = '数据异常';\n\t\t\treturn json(\$data);\n\t\t\texit;\n\t\t}\n";
            $html.=$biao2."\$data = array('status' => 0,'msg' => '未知错误');\n".$biao2."\$array=input(\"param.id\");\n".$biao2."if(!\$array){\n".$biao3."\$data['msg']='参数错误';return json(\$data);exit;\n".$biao2."}\n";
            $html.=$biao2."\$arr=explode(\",\",\$array);\n".$biao2."for(\$i=0;\$i<count(\$arr);\$i++){\n";
            $html.=$biao3."if(\$arr[\$i]){\n".$biao4."Db::name('".$table."')->where('merid',\$merid)->where('id',\$arr[\$i])->delete();\n".$biao4."\$text = '删除了".$title."id='.\$arr[\$i].'的数据';\n".$biao4."\$this->writeActionLog(\$text);\n".$biao3."}\n";
            $html.=$biao2."}\n".$biao2."\$data['status']=1;\n".$biao2."return json(\$data);\n";
            $html.=$biao1."}\n";

            //$html.="\n} ";

            //在文件末尾的第二行添加代码
            $handle = fopen($path, 'r+');
            $i = -1;
            $lastLine = '';
            while(true){
                fseek($handle, $i, SEEK_END);
                $char = fgetc($handle);
                if($char == "\n"){
                    fwrite($handle, $html. $lastLine);
                    break;
                }else{
                    $lastLine .= $char;
                }
                $i --;
            }

        }

        //生成静态文件
        $this->codehtml($model,$action,$pararr,$title,$table);
    }
    /**
     * 生成html文件
     **/
    protected function codehtml($model,$action,$pararr,$title,$table){
        //先判断列表文件
        $models=strtolower($model);
        $path2=app()->getRootPath().'/addons/merchants/view/'.$models.'/'.$action.'.html';
        $mk=app()->getRootPath().'/addons/merchants/view/'.$models;
        if(!file_exists($path2)){
            if(!is_dir($mk)){
                mkdir(iconv("UTF-8", "GBK", $mk),0777,true);
            }
            //顶部代码
            $html="<!DOCTYPE html>\n<html>\n<head>\n\t<meta charset=\"utf-8\">\n\t<title>管理</title>\n\t<meta name=\"renderer\" content=\"webkit\">\n\t<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\">\n\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0\">\n\t<link rel=\"stylesheet\" href=\"/admin/layuiadmin/layui/css/layui.css\" media=\"all\">\n\t<link rel=\"stylesheet\" href=\"/admin/layuiadmin/style/admin.css\" media=\"all\">\n\t<style>\n\t\t@font-face {\n\t\t\tfont-family: 'iconfont';  \n\t\t\tsrc: url('https://at.alicdn.com/t/font_2565954_q2snf28joy.woff2?t=1621766073287') format('woff2'),\n\t\t\turl('https://at.alicdn.com/t/font_2565954_q2snf28joy.woff?t=1621766073287') format('woff'),\n\t\t\turl('https://at.alicdn.com/t/font_2565954_q2snf28joy.ttf?t=1621766073287') format('truetype');\n\t\t}\n\t\t.iconfont{\n\t\t\tfont-family:\"iconfont\" !important;\n\t\t\tfont-size:16px;font-style:normal;\n\t\t\t-webkit-font-smoothing: antialiased;\n\t\t\t-webkit-text-stroke-width: 0.2px;\n\t\t\t-moz-osx-font-smoothing: grayscale;}\n\t\t.layui-layer-admin .layui-layer-ico {\n\t\t\tbackground: url(\"/admin/layuiadmin/gb.png\") no-repeat!important;\n\t\t\tbackground-size:16px 16px!important;\n\t\t}\n\t</style>\n</head>\n<body>\n";
            //中间代码
            $html.="<div class=\"layui-fluid\">\n\t<div class=\"layui-card\">\n\t\t<div class=\"layui-form layui-card-header layuiadmin-card-header-auto\">\n\t\t\t<div class=\"layui-form-item\">\n\t\t\t\t<div class=\"layui-inline\">\n\t\t\t\t\t<label class=\"layui-form-label\">ID</label>\n\t\t\t\t\t<div class=\"layui-input-block\">\n\t\t\t\t\t\t<input type=\"text\" name=\"id\" placeholder=\"请输入\" autocomplete=\"off\" class=\"layui-input\">\n\t\t\t\t\t</div>\n\t\t\t\t</div>\n\t\t\t\t<div class=\"layui-inline\">\n\t\t\t\t\t<button class=\"layui-btn layuiadmin-btn-useradmin\" lay-submit lay-filter=\"LAY-user-front-search\">\n\t\t\t\t\t\t<i class=\"layui-icon layui-icon-search layuiadmin-button-btn\"></i>\n\t\t\t\t\t</button>\n\t\t\t\t</div>\n\t\t\t</div>\n\t\t</div>\n\t\t<div class=\"layui-card-body\">\n\t\t\t<div style=\"padding-bottom: 10px;\">\n\t\t\t{in name=\"\$data.add".$action."\" value=\"\$permis\"}\n\t\t\t\t<button class=\"layui-btn layuiadmin-btn-useradmin\" data-type=\"add\">添加</button>\n\t\t\t{/in}\n\t\t\t{in name=\"\$data.del".$action."\" value=\"\$permis\"}\n\t\t\t\t<button class=\"layui-btn layuiadmin-btn-useradmin\" data-type=\"del\">删除</button>\n\t\t\t{/in}\n\t\t\t</div>\n\t\t\t<table class=\"layui-table\" lay-data=\"{ url:'/addons/merchants/".$model."/".$action."',method:'post', page:true, id:'LAY-user-managet'}\" lay-filter=\"LAY-user-managet\">\n\t\t\t<thead>\n\t\t\t\t<tr>\n";

            $html.="\t\t\t\t\t<th lay-data=\"{type: 'checkbox', fixed: 'left'}\"></th>\n";
            $html.="\t\t\t\t\t<th lay-data=\"{field:'id', width:110, sort: true}\">ID</th>\n";
            if($pararr){
                for ($i=0;$i<count($pararr);$i++){
                    $titlex=$pararr[$i]['title'];
                    $name=$pararr[$i]['name'];
                    $html.="\t\t\t\t\t<th lay-data=\"{field:'".$titlex."'}\">".$name."</th>\n";
                }
            }
            $html.="\t\t\t\t\t<th lay-data=\"{fixed: 'right', width:220, align:'center', toolbar: '#barDemo'}\">操作</th>\n\t\t\t\t</tr>\n\t\t\t</thead>\n\t\t\t</table>\n\t\t\t<script type=\"text/html\" id=\"barDemo\">\n\t\t\t{in name=\"\$data.edit".$action."\" value=\"\$permis\"}\n\t\t\t\t<a class=\"layui-btn layui-btn-normal layui-btn-xs\" lay-event=\"edit\"><i class=\"layui-icon layui-icon-edit\"></i>编辑</a>\n\t\t\t{/in}\n\t\t\t{in name=\"\$data.del".$action."\" value=\"\$permis\"}\n\t\t\t\t<a class=\"layui-btn layui-btn-danger layui-btn-xs\" lay-event=\"del\"><i class=\"layui-icon layui-icon-delete\"></i>删除</a>\n\t\t\t{/in}\n\t\t\t</script>\n\t\t</div>\n\t</div>\n</div>\n";
            //底部js部分
            $html.="<script src=\"/admin/layuiadmin/layui/layui.js\"></script>\n<script>\n";
            $html.="\tlayui.config({\n\t\tbase: '/admin/layuiadmin/' \n\t}).extend({\n\t\tindex: 'lib/index' \n\t}).use(['index', 'useradmin', 'table'], function(){\n\t\tvar $ = layui.$,form = layui.form,table = layui.table;\n";
            /**搜索**/
            $html.="\t\tform.on('submit(LAY-user-front-search)', function(data){\n\t\t\tvar field = data.field;\n\t\t\t$.post(\"{:url('/addons/merchants/".$model."/".$action."')}\", field, function(data,state){});\n\t\t\t//执行重载\n\t\t\ttable.reload('LAY-user-managet', {\n\t\t\t\twhere: field\n\t\t\t});\n\t\t});\n";
            /**操作按钮**/
            $html.="\t\ttable.on('tool(LAY-user-managet)', function(obj){\n\t\t\tvar data = obj.data; //获得当前行数据\n\t\t\tvar layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）\n\t\t\tvar tr = obj.tr; //获得当前行 tr 的DOM对象\n";
            $html.="\t\t\tif(layEvent === 'del'){ //删除\n\t\t\t\tlayer.confirm('真的删除当前数据吗', function(index){\n\t\t\t\t\tobj.del(); //删除对应行（tr）的DOM结构，并更新缓存\n\t\t\t\t\tlayer.close(index);\n\t\t\t\t\tvar dataid=data['id'];\n\t\t\t\t\t//向服务端发送删除指令\n\t\t\t\t\t$.post(\n\t\t\t\t\t\t\"{:url('/addons/merchants/".$model."/del".$action."')}\",\n\t\t\t\t\t\t{\"id\":dataid},\n\t\t\t\t\t\tfunction(data,state){\n\t\t\t\t\t\t\tif(state != \"success\"){\n\t\t\t\t\t\t\t\tlayer.msg(\"请求出错!\");\n\t\t\t\t\t\t\t}else if(data.status == 1){\n\t\t\t\t\t\t\t\ttable.reload('LAY-user-managet');\n\t\t\t\t\t\t\t\tlayer.msg('已删除');\n\t\t\t\t\t\t\t}else{\n\t\t\t\t\t\t\t\tlayer.msg(data.msg);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t);\n\t\t\t\t});\n\t\t\t}\n";
            /**修改**/
            $html.="\t\t\telse if(layEvent === 'edit'){\n\t\t\t\tvar id=data['id'];\n\t\t\t\tlayer.open({\n\t\t\t\t\ttype: 2\n\t\t\t\t\t,title: '修改".$title."'\n\t\t\t\t\t,content: 'edit".$action.".html'\n\t\t\t\t\t,maxmin: true\n\t\t\t\t\t,area: ['600px', '450px']\n\t\t\t\t\t,btn: ['确定', '取消']\n\t\t\t\t\t,success: function(layero,index){\n\t\t\t\t\t\tvar body = layui.layer.getChildFrame('body', index);\n\t\t\t\t\t\tbody.find(\"#id\").val(id);\n";
            if($pararr) {
                for ($i=0;$i<count($pararr);$i++) {
                    $titlex=$pararr[$i]['title'];//字段名
                    $style=$pararr[$i]['style'];
                    $val=$pararr[$i]['val'];//表单值
                    if($style==1) { //文本框
                        $html .= "\t\t\t\t\t\tbody.find(\"#" . $titlex . "\").val(data['" . $titlex . "']);\n";
                    }else if($style==2){ //单选框
                        $valarr=explode(',',$val);
                        if($valarr && count($valarr)>0){
                            for ($d=0;$d<count($valarr);$d++){
                                $html.="\t\t\t\t\t\tbody.find(\"input[name=" . $titlex . "][value=".$valarr[$d]."]\").attr(\"checked\", data['" . $titlex . "'] == ".$valarr[$d]." ? true : false);\n";
                            }
                        }else{
                            $html.="\t\t\t\t\t\tbody.find(\"input[name=" . $titlex . "][value=".$val."]\").attr(\"checked\", data['" . $titlex . "'] == ".$val." ? true : false);\n";
                        }
                    }
                }
            }
            $html.="\t\t\t\t\t\tform.render();\n\t\t\t\t\t}\n\t\t\t\t\t,yes: function(index, layero){\n\t\t\t\t\t\tvar iframeWindow = window['layui-layer-iframe'+ index],submitID = 'LAY-user-front-submit',submit = layero.find('iframe').contents().find('#'+ submitID);\n\t\t\t\t\t\t//监听提交\n\t\t\t\t\t\tiframeWindow.layui.form.on('submit('+ submitID +')', function(data){\n\t\t\t\t\t\t\tvar field = data.field; //获取提交的字段\n\t\t\t\t\t\t\t//提交 Ajax 成功后，静态更新表格中的数据\n\t\t\t\t\t\t\t//$.ajax({});\n\t\t\t\t\t\t\t$.post(\n\t\t\t\t\t\t\t\t\"{:url('/addons/merchants/".$model."/edit".$action."')}\",\n\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\tfunction(data,state){\n\t\t\t\t\t\t\t\t\tif(state != \"success\"){\n\t\t\t\t\t\t\t\t\t\tlayer.msg(\"请求出错!\");\n\t\t\t\t\t\t\t\t\t}else if(data.status == 1){\n\t\t\t\t\t\t\t\t\t\tlayer.msg('修改成功');\n\t\t\t\t\t\t\t\t\t\ttable.reload('LAY-user-managet'); //数据刷新\n\t\t\t\t\t\t\t\t\t}else{\n\t\t\t\t\t\t\t\t\t\tlayer.msg(data.msg);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tlayer.close(index); //关闭弹层\n\t\t\t\t\t\t});\n\t\t\t\t\t\tsubmit.trigger('click');\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n";
            $html.="\t\t});\n";
            /**操作按钮结束**/

            /**事件**/
            $html.="\t\tvar active = {\n";
            /**添加**/
            $html.="\t\t\tadd:function(){\n\t\t\t\tlayer.open({\n\t\t\t\t\ttype: 2\n\t\t\t\t\t,title: '添加".$title."'\n\t\t\t\t\t,content: 'add".$action.".html'\n\t\t\t\t\t,maxmin: true\n\t\t\t\t\t,area: ['600px', '450px']\n\t\t\t\t\t,btn: ['确定', '取消']\n\t\t\t\t\t,yes: function(index, layero){\n\t\t\t\t\t\tvar iframeWindow = window['layui-layer-iframe'+ index]\n\t\t\t\t\t\t\t,submitID = 'LAY-user-front-submit'\n\t\t\t\t\t\t\t,submit = layero.find('iframe').contents().find('#'+ submitID);\n\t\t\t\t\t\t//监听提交\n\t\t\t\t\t\tiframeWindow.layui.form.on('submit('+ submitID +')', function(data){\n\t\t\t\t\t\t\tvar field = data.field; //获取提交的字段\n\t\t\t\t\t\t\t//提交 Ajax 成功后，静态更新表格中的数据\n\t\t\t\t\t\t\t//$.ajax({});\n\t\t\t\t\t\t\t$.post(\n\t\t\t\t\t\t\t\t\"{:url('/addons/merchants/".$model."/add".$action."')}\",\n\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\tfunction(data,state){\n\t\t\t\t\t\t\t\t\tif(state != \"success\"){\n\t\t\t\t\t\t\t\t\t\tlayer.msg(\"请求出错!\");\n\t\t\t\t\t\t\t\t\t}else if(data.status == 1){\n\t\t\t\t\t\t\t\t\t\tlayer.msg('添加成功');\n\t\t\t\t\t\t\t\t\t\ttable.reload('LAY-user-managet'); //数据刷新\n\t\t\t\t\t\t\t\t\t}else{\n\t\t\t\t\t\t\t\t\t\tlayer.msg(data.msg);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tlayer.close(index); //关闭弹层\n\t\t\t\t\t\t});\n\t\t\t\t\t\tsubmit.trigger('click');\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t},\n";
            /**删除**/
            $html.="\t\t\tdel: function(){\n\t\t\t\tvar checkStatus = table.checkStatus('LAY-user-managet'),checkData = checkStatus.data; //得到选中的数据\n\t\t\t\tif(checkData.length === 0){\n\t\t\t\t\treturn layer.msg('请选择数据');\n\t\t\t\t}\n\t\t\t\tvar dataid='';\n\t\t\t\tfor(var i=0;i<checkData.length;i++){\n\t\t\t\t\tdataid=dataid+','+checkData[i]['id'];\n\t\t\t\t}\n\t\t\t\tlayer.confirm('确定删除选中数据吗？', function(index) {\n\t\t\t\t\t$.post(\n\t\t\t\t\t\t\"{:url('/addons/merchants/".$model."/del".$action."')}\",\n\t\t\t\t\t\t{\"id\":dataid},\n\t\t\t\t\t\tfunction(data,state){\n\t\t\t\t\t\t\tif(state != \"success\"){\n\t\t\t\t\t\t\t\tlayer.msg(\"请求出错!\");\n\t\t\t\t\t\t\t}else if(data.status == 1){\n\t\t\t\t\t\t\t\ttable.reload('LAY-user-managet');\n\t\t\t\t\t\t\t\tlayer.msg('已删除');\n\t\t\t\t\t\t\t}else{\n\t\t\t\t\t\t\t\tlayer.msg(data.msg);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t);\n\t\t\t\t});\n\t\t\t}\n";
            $html.="\t\t}\n";
            /**事件结束**/
            $html.="\t\t$('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){\n\t\t\tvar type = $(this).data('type');\n\t\t\tactive[type] ? active[type].call(this) : '';\n\t\t});\n";
            $html.="\t});\n";
            $html.="</script>\n</body>\n</html>";
            file_put_contents($path2,$html);
        }

        //生成修改文件
        $path3=app()->getRootPath().'/addons/merchants/view/'.$model.'/edit'.$action.'.html';
        $mk=app()->getRootPath().'/addons/merchants/view/'.$model;
        if(!file_exists($path3)) {
            if (!is_dir($mk)) {
                mkdir(iconv("UTF-8", "GBK", $mk), 0777, true);
            }
            /**顶部代码**/
            $html="<!DOCTYPE html>\n<html>\n<head>\n\t<meta charset=\"utf-8\">\n\t<title>管理</title>\n\t<meta name=\"renderer\" content=\"webkit\">\n\t<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\">\n\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0\">\n\t<link rel=\"stylesheet\" href=\"/admin/layuiadmin/layui/css/layui.css\" media=\"all\">\n\t<link rel=\"stylesheet\" href=\"/admin/css/u-ui.css\" media=\"all\">\n</head>\n<body>\n";
            /**中间部分代码**/
            $html.="<div class=\"layui-form\" lay-filter=\"layuiadmin-form-useradmin\" id=\"layuiadmin-form-useradmin\" style=\"padding: 20px 0 0 0;\">\n\t<input type=\"hidden\" name=\"id\" id=\"id\" value=\"\" />\n\t";
            if($pararr) {
                for ($i=0;$i<count($pararr);$i++) {
                    $name=$pararr[$i]['name'];
                    $titlex=$pararr[$i]['title'];//字段名
                    $style=$pararr[$i]['style'];
                    $val=$pararr[$i]['val'];//表单值
                    if($style==1){ //文本框
                        $html.="<div class=\"layui-form-item\">\n\t\t<label class=\"layui-form-label\">".$name."</label>\n\t\t<div class=\"layui-input-inline\">\n\t\t\t<input type=\"text\" name=\"".$titlex."\" id=\"".$titlex."\" lay-verify=\"required\"  placeholder=\"".$name."\" autocomplete=\"off\" class=\"layui-input\">\n\t\t</div>\n\t</div>\n\t";
                    }else if($style==2){
                        $valarr=explode(',',$val);
                        if($valarr && count($valarr)>0){
                            $html.="<div class=\"layui-form-item\" lay-filter=\"status\">\n\t\t<label class=\"layui-form-label\">".$name."</label>\n\t\t<div class=\"layui-input-block\">\n";
                            for ($d=0;$d<count($valarr);$d++){
                                $html.="\t\t\t<input type=\"radio\" name=\"".$titlex."\" checked  id=\"".$titlex."\" value=\"".$valarr[$d]."\" title=\"上线\">\n";
                            }
                            $html.="\t\t</div>\n\t</div>\n\t";
                        }else{
                            $html.="<div class=\"layui-form-item\" lay-filter=\"status\">\n\t\t<label class=\"layui-form-label\">".$name."</label>\n\t\t<div class=\"layui-input-block\">\n";
                            $html.="\t\t\t<input type=\"radio\" name=\"".$titlex."\" checked  id=\"".$titlex."\" value=\"".$val."\" title=\"上线\">\n";
                            $html.="\t\t</div>\n\t</div>\n\t";
                        }
                    }
                }
            }
            $html.="\n\t<div class=\"layui-form-item layui-hide\">\n\t\t<input type=\"button\" lay-submit lay-filter=\"LAY-user-front-submit\" id=\"LAY-user-front-submit\" value=\"确认\">\n\t</div>\n</div>\n";
            $html.="<script src=\"/admin/layuiadmin/layui/layui.js\"></script>\n<script>\n";
            $html.="\tlayui.config({\n\t\tbase: '/admin/layuiadmin/' \n\t}).extend({\n\t\tindex: 'lib/index' \n\t}).use(['index', 'useradmin', 'table','upload'], function(){\n\t\tvar $ = layui.$,form = layui.form,upload = layui.upload,table = layui.table;\n";
            $html.="\t});\n";
            $html.="</script>\n</body>\n</html>";
            file_put_contents($path3,$html);
        }
    }
    /**顶级栏目生成**/
    public function topnav(){
        if(request()->isPost()){
            $page=input('param.page');
            if(!$page){
                $page=1;
            }
            $limit=input('param.limit');
            if(!$limit){
                $limit=10;//每页显示条数
            }
            $where=[];
            $id=input('param.id');
            if($id){
                $where[]=['id','=',$id];
            }
            $where[]=['parent_id','=',0];
            $count = Db::name('escort_admin_permission')->where($where)->count();
            $data=Db::name('escort_admin_permission')->where($where)->page($page,$limit)->order('id desc')->select()->toArray();
            $res = ["data" => $data,"code"=>0,'message'=>'请求成功','count'=>$count];
            return json($res);exit;
        }
        $name='topnav';
        $permis = $this->getPermissions('Autocurd/topnav');
        $actions = $this->actions;
        $assign=['data'=>$actions,'permis'=>$permis];
        return View::fetch($name,$assign);
    }
    public function addtopnav(){
        if(request()->isPost()){
            $data = array('status' => 0, 'msg' => '未知错误');
            $name=input('param.name');
            $controllers=input('param.models');
            $icon=input('param.icon');
            $p_id=input('param.p_id');
            $type=input('param.type');
            $addons=input('param.addons');
            $arr=array(
                'name'=>$name,
                'controller'=>$controllers,
                'parent_id'=>0,
                'is_nav'=>1,
                'icon'=>$icon,
                'p_id'=>$p_id,
                'type'=>$type,
                'addons'=>$addons,
            );
            $id=Db::name('escort_admin_permission')->insertGetId($arr);
            $text = '添加了顶级栏目生成-id='.$id;
            $this->writeActionLog($text);
            $data['status'] = 1;
            return json($data);exit;
        }
        $name='edittopnav';
        $assign=[];
        return View::fetch($name,$assign);
    }
    public function edittopnav(){
        if(request()->isPost()){
            $data = array('status' => 0, 'msg' => '未知错误');
            $name=input('param.name');
            $controllers=input('param.models');
            $icon=input('param.icon');
            $p_id=input('param.p_id');
            $type=input('param.type');
            $addons=input('param.addons');
            $arr=array(
                'name'=>$name,
                'controller'=>$controllers,
                'icon'=>$icon,
                'p_id'=>$p_id,
                'type'=>$type,
                'addons'=>$addons,
            );
            $id = input('param.id');
            if ($id){
                $id=Db::name('escort_admin_permission')->where('id',$id)->update($arr);
                $text = '修改了顶级栏目生成-id='.$id;
                $this->writeActionLog($text);
                $data['status'] = 1;
                return json($data);exit;
            }
        }
        $name='edittopnav';
        $assign=[];
        return View::fetch($name,$assign);
    }
    public function deltopnav(){
        $data = array('status' => 0,'msg' => '未知错误');
        $array=input("param.id");
        if(!$array){
            $data['msg']='参数错误';return json($data);exit;
        }
        $arr=explode(",",$array);
        for($i=0;$i<count($arr);$i++){
            if($arr[$i]){
                Db::name('escort_admin_permission')->where('parent_id',0)->where('id',$arr[$i])->delete();
                $text = '删除了顶级栏目生成id='.$arr[$i].'的数据';
                $this->writeActionLog($text);
            }
        }
        $data['status']=1;
        return json($data);
    }



    /**二级栏目生成**/
    /**一键生成curd**/
    public function curdlist(){
        if(request()->isPost()){
            $page=input("param.page");
            if(!$page){
                $page=1;
            }
            $where=[];
            $id=input('param.id');
            if($id){
                $where[]=['id','=',$id];
            }
            $title=input('param.title');
            if($title){
                $where[]=['title','like','%'.$title.'%'];
            }
            $limit=input("param.limit");
            if(!$limit){
                $limit=10;//每页显示条数
            }
            //查询产品
            $count = Db::name('ohyueo_develo_curdlist')
                ->where($where)
                ->count();
            $data=Db::name('ohyueo_develo_curdlist')
                ->where($where)
                ->page($page,$limit)
                ->order('id desc')
                ->select()->toArray();
            if($data){
                for($i=0;$i<count($data);$i++){
                    $id=$data[$i]['id'];//
                    $data[$i]['no']=0;
                }
            }
            $res = ["data" => $data,"code"=>0,'message'=>'请求成功','count'=>$count];
            return json($res);exit;
        }
        $name='curdlist';
        $permis = $this->getPermissions('Autocurd/curdlist');
        $actions = $this->actions;
        $assign=['data'=>$actions,'permis'=>$permis];
        return View::fetch($name,$assign);
    }
    public function addcurd(){
        if(request()->isPost()){
            $title=input('param.title');
            $model=input('param.model');
            if($model){
                //先转换为小写
                $model=strtolower($model);
                //转换为首字母大写其他小写
                $model=ucfirst($model);
            }
            $action=input('param.actions');
            if($action){
                //需要强制转换action为小写 因为要生成模型
                $action=strtolower($action);
            }
            $frontapi=input('param.frontapi')?:'';//前端api
            $table=input('param.table');
            $beizhu=input('param.beizhus');
            $navid=input('param.navid');//一级菜单id
            $role_type=input('param.role_type');//生成权限
            $daochu=input('param.daochu');//导出
            if(!$table || $table=='escort_'){
                $table='escort_'.$model.'_'.$action;
                $table=strtolower($table);
            }
            $listid=0;
            $isc=Db::name('ohyueo_develo_curdlist')->where('model',$model)->where('action',$action)->find();
            if(!$isc){
                $arr=array(
                    'title'=>$title,'model'=>$model,'action'=>$action,'beizhu'=>$beizhu,'navid'=>$navid,'role_type'=>$role_type,
                    'table'=>$table,'addtime'=>gettime(),'daochu'=>$daochu
                );
                $listid=Db::name('ohyueo_develo_curdlist')->insertGetId($arr);
            }
            $name=input('param.name');
            $type=input('param.type');
            $fid=input('param.fid');
            $length=input('param.length');
            $default=input('param.default');
            $style=input('param.style');
            $val=input('param.val');
            $beizhux=input('param.beizhu');
            $search=input('param.search');
            $newarr=[];//生成数据
            if($listid && $table && count($name)>0){
                $sql = "CREATE TABLE `".$table."` ( `id` int(11) NOT NULL AUTO_INCREMENT,";
                $sql2 = "\nCREATE TABLE `".$table."` ( \n`id` int(11) NOT NULL AUTO_INCREMENT,\n";//为了记录表sql需要换行
                for($i=0;$i<count($name);$i++){
                    $gid=$fid[$i];
                    $iseach=0;
                    if(isset($search[$i]) && $search[$i]=='on'){
                        $iseach=1;
                    }
                    array_push($newarr,array('title'=>$name[$i],'type'=>$type[$i],'name'=>$beizhux[$i],'style' => $style[$i],'val' => $val[$i],'search'=>$iseach));
                    $arr=array(
                        'listid' => $listid,
                        'name'  => $name[$i],
                        'type' => $type[$i],
                        'length' => $length[$i],
                        'default' => $default[$i],
                        'style' => $style[$i],
                        'val' => $val[$i],
                        'beizhu' => $beizhux[$i],
                        'search' => $iseach,
                    );
                    if($gid){//修改
                        Db::name('ohyueo_develo_curdtable')->where('id',$gid)->update($arr);
                        $data['msg']='修改成功';
                        $data['status']=1;
                        $text = '修改了curd表信息 id='.$gid;
                        $this->writeActionLog($text);
                    }else{//添加
                        $gid=Db::name('ohyueo_develo_curdtable')->insertGetId($arr);
                        $text = '添加了curd表信息id='.$gid;
                        $this->writeActionLog($text);
                        $data['msg']='添加成功';
                        $data['status']=1;
                    }
                    //拼接sql
                    if($type[$i]==1){
                        $moren=$default[$i]?:NULL;
                        $sql.="`".$name[$i]."` varchar(".$length[$i].") DEFAULT '".$moren."' COMMENT '".$beizhux[$i]."',";
                        $sql2.="`".$name[$i]."` varchar(".$length[$i].") DEFAULT '".$moren."' COMMENT '".$beizhux[$i]."',\n";
                    }else if($type[$i]==2){
                        $moren=$default[$i]?$default[$i]:'0';
                        $sql.="`".$name[$i]."` int(".$length[$i].") DEFAULT '".$moren."' COMMENT '".$beizhux[$i]."',";
                        $sql2.="`".$name[$i]."` int(".$length[$i].") DEFAULT '".$moren."' COMMENT '".$beizhux[$i]."',\n";
                    }else if($type[$i]==3){
                        $moren=$default[$i]?:NULL;
                        $sql.="`".$name[$i]."` text COMMENT '".$beizhux[$i]."',";
                        $sql2.="`".$name[$i]."` text COMMENT '".$beizhux[$i]."',\n";
                    }else if($type[$i]==4){
                        $moren=$default[$i]?:'0';
                        $sql.="`".$name[$i]."` decimal(".$length[$i].") DEFAULT '".$moren."' COMMENT '".$beizhux[$i]."',";
                        $sql2.="`".$name[$i]."` decimal(".$length[$i].") DEFAULT '".$moren."' COMMENT '".$beizhux[$i]."',\n";
                    }else if($type[$i]==5){
                        $moren=NULL;
                        $sql.="`".$name[$i]."` date DEFAULT ".$moren." COMMENT '".$beizhux[$i]."',";
                        $sql2.="`".$name[$i]."` date DEFAULT ".$moren." COMMENT '".$beizhux[$i]."',\n";
                    }else if($type[$i]==6){
                        $moren=NULL;
                        $sql.="`".$name[$i]."` datetime DEFAULT ".$moren." COMMENT '".$beizhux[$i]."',";
                        $sql2.="`".$name[$i]."` datetime DEFAULT ".$moren." COMMENT '".$beizhux[$i]."',\n";
                    }
                }
                //merid  `merid` int(11) NOT NULL DEFAULT '0',
                $sql.="`merid` int(11) DEFAULT '0' COMMENT '租户id',";
                $sql2.="`merid` int(11) DEFAULT '0' COMMENT '租户id',\n";
                
                $sql.=" PRIMARY KEY (`id`) ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COMMENT='".$beizhu."';";
                $sql2.=" PRIMARY KEY (`id`) \n) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COMMENT='".$beizhu."';\n";
                Db::query($sql);//创建数据库
                //将数据库sql语句记录到auto.sql文件中
                $path=app()->getRootPath().'/auto.sql';
                //将$sql写入到文件中
                file_put_contents($path,$sql2,FILE_APPEND);
            }

            if($role_type==1){
                $sql2='';//需要记录权限表sql
                //先生成权限表数据
                $ro=Db::name('escort_admin_permission')->where('parent_id',$navid)->where('controller',$model)->where('action',$action)->find();
                if(!$ro){
                    $roarr=array(
                        'name' => $title,
                        'controller' => $model,
                        'action' => $action,
                        'parent_id' => $navid,
                        'is_nav'=>1,
                        'icon' => '',
                        'p_id' => 0,
                        'type' => 1,
                        'addons' => ''
                    );
                    $rid=Db::name('escort_admin_permission')->insertGetId($roarr);
                    $sql2.="INSERT INTO `escort_admin_permission` (`name`, `controller`, `action`, `parent_id`, `is_nav`, `icon`, `p_id`, `type`, `addons`) VALUES ('".$title."', '".$model."', '".$action."', '".$navid."', '1', '', '0', '1', '');\n";
                    //添加  修改  删除
                    $roarr=array(
                        'name' => '添加',
                        'controller' => $model.'/'.$action,
                        'action' => 'add'.$action,
                        'parent_id' => $rid,
                        'is_nav'=>0,
                        'icon' => '',
                        'p_id' => 0,
                        'type' => 1,
                        'addons' => ''
                    );
                    $rid2=Db::name('escort_admin_permission')->insertGetId($roarr);
                    $sql2.="INSERT INTO `escort_admin_permission` (`name`, `controller`, `action`, `parent_id`, `is_nav`, `icon`, `p_id`, `type`, `addons`) VALUES ('添加', '".$model.'/'.$action."', 'add".$action."', '".$rid."', '0', '', '0', '1', '');\n";
                    //修改
                    $roarr=array(
                        'name' => '修改',
                        'controller' => $model.'/'.$action,
                        'action' => 'edit'.$action,
                        'parent_id' => $rid,
                        'is_nav'=>0,
                        'icon' => '',
                        'p_id' => 0,
                        'type' => 1,
                        'addons' => ''
                    );
                    $rid3=Db::name('escort_admin_permission')->insertGetId($roarr);
                    $sql2.="INSERT INTO `escort_admin_permission` (`name`, `controller`, `action`, `parent_id`, `is_nav`, `icon`, `p_id`, `type`, `addons`) VALUES ('修改', '".$model.'/'.$action."', 'edit".$action."', '".$rid."', '0', '', '0', '1', '');\n";
                    //删除
                    $roarr=array(
                        'name' => '删除',
                        'controller' => $model.'/'.$action,
                        'action' => 'del'.$action,
                        'parent_id' => $rid,
                        'is_nav'=>0,
                        'icon' => '',
                        'p_id' => 0,
                        'type' => 1,
                        'addons' => ''
                    );
                    $rid4=Db::name('escort_admin_permission')->insertGetId($roarr);
                    $sql2.="INSERT INTO `escort_admin_permission` (`name`, `controller`, `action`, `parent_id`, `is_nav`, `icon`, `p_id`, `type`, `addons`) VALUES ('删除', '".$model.'/'.$action."', 'del".$action."', '".$rid."', '0', '', '0', '1', '');\n";
                    //将数据库sql语句记录到auto.sql文件中
                    $path=app()->getRootPath().'/auto.sql';
                    //将$sql写入到文件中
                    file_put_contents($path,$sql2,FILE_APPEND);
                    //将这些权限添加到超级管理员中
                    $permission_id=Db::name('escort_admin_role_permission')->where('role_id',1)->where('id',1)->value('permission_id');
                    if($permission_id){
                        $permission_id=$permission_id.','.$rid.','.$rid2.','.$rid3.','.$rid4;
                    }else{
                        $permission_id=$rid.','.$rid2.','.$rid3.','.$rid4;
                    }
                    Db::name('escort_admin_role_permission')->where('role_id',1)->where('id',1)->update(['permission_id'=>$permission_id]);
                }
            }
            //生成php文件
            $this->modelaction($model,$action,$newarr,$title,$table,$daochu,$frontapi);
            //生成前端php文件
            $this->apimodelaction($model,$action,$newarr,$title,$table,$daochu,$frontapi);

            $data['status']=1;
            $data['msg']='添加成功';
            return json($data);exit;
        }
        //查询所有一级菜单
        $navlist=Db::name('escort_admin_permission')->where('parent_id',0)->select()->toArray();
        $name='addcurd';
        $assign=['navlist'=>$navlist,'list'=>[]];
        return View::fetch($name,$assign);
    }
    public function editcurd(){
        if(request()->isPost()){
            $title=input('param.title');
            $model=input('param.model');
            if($model){
                //先转换为小写
                $model=strtolower($model);
                //转换为首字母大写其他小写
                $model=ucfirst($model);
            }
            $action=input('param.actions');
            if($action){
                //需要强制转换action为小写 因为要生成模型
                $action=strtolower($action);
            }
            $table=input('param.table');
            $beizhu=input('param.beizhus');
            $navid=input('param.navid');//一级菜单id
            $role_type=input('param.role_type');//生成权限
            $daochu=input('param.daochu')?:0;//导出
            if(!$table || $table=='escort_'){
                $table='escort_'.$model.'_'.$action;
                $table=strtolower($table);
            }
            $frontapi=input('param.frontapi')?:'';//前端api

            $listid=0;
            $isc=Db::name('ohyueo_develo_curdlist')->where('model',$model)->where('action',$action)->find();
            if(!$isc){
                $arr=array(
                    'title'=>$title,'model'=>$model,'action'=>$action,'beizhu'=>$beizhu,'navid'=>$navid,'role_type'=>$role_type,
                    'table'=>$table,'addtime'=>gettime(),'daochu'=>$daochu
                );
                $listid=Db::name('ohyueo_develo_curdlist')->insertGetId($arr);
            }else{
                $listid=$isc['id'];
                $arr=array(
                    'title'=>$title,'model'=>$model,'action'=>$action,'beizhu'=>$beizhu,'navid'=>$navid,'role_type'=>$role_type,
                    'table'=>$table,'addtime'=>gettime(),'daochu'=>$daochu
                );
                Db::name('ohyueo_develo_curdlist')->where('id',$listid)->update($arr);
            }
            $name=input('param.name');
            $type=input('param.type');
            $fid=input('param.fid');
            $length=input('param.length');
            $default=input('param.default');
            $style=input('param.style');
            $val=input('param.val');
            $beizhux=input('param.beizhu');
            $search=input('param.search');
            $newarr=[];//生成数据
            if(count($name)>0){
                /*如果table存在则拼接sql语句*/
                if($table){
                    $sql = "CREATE TABLE `".$table."` ( `id` int(11) NOT NULL AUTO_INCREMENT,";
                }
                for($i=0;$i<count($name);$i++){
                    $gid=$fid[$i];
                    $iseach=0;
                    if(isset($search[$i]) && $search[$i]=='on'){
                        $iseach=1;
                    }
                    array_push($newarr,array('title'=>$name[$i],'type'=>$type[$i],'name'=>$beizhux[$i],'style' => $style[$i],'val' => $val[$i],'search'=>$iseach));
                    $arr=array(
                        'listid' => $listid,
                        'name'  => $name[$i],
                        'type' => $type[$i],
                        'length' => $length[$i],
                        'default' => $default[$i],
                        'style' => $style[$i],
                        'val' => $val[$i],
                        'beizhu' => $beizhux[$i],
                        'search' => $iseach,
                    );
                    if($gid){//修改
                        Db::name('ohyueo_develo_curdtable')->where('id',$gid)->update($arr);
                        $data['msg']='修改成功';
                        $data['status']=1;
                        $text = '修改了curd表信息 id='.$gid;
                        $this->writeActionLog($text);
                    }else{//添加
                        $gid=Db::name('ohyueo_develo_curdtable')->insertGetId($arr);
                        $text = '添加了curd表信息id='.$gid;
                        $this->writeActionLog($text);
                        $data['msg']='添加成功';
                        $data['status']=1;
                    }
                    if($table){
                        //拼接sql
                        if($type[$i]==1){
                            $moren=$default[$i]?:NULL;
                            $sql.="`".$name[$i]."` varchar(".$length[$i].") DEFAULT '".$moren."' COMMENT '".$beizhux[$i]."',";
                        }else if($type[$i]==2){
                            $moren=$default[$i]?$default[$i]:'0';
                            $sql.="`".$name[$i]."` int(".$length[$i].") DEFAULT '".$moren."' COMMENT '".$beizhux[$i]."',";
                        }else if($type[$i]==3){
                            $moren=$default[$i]?:NULL;
                            $sql.="`".$name[$i]."` text COMMENT '".$beizhux[$i]."',";
                        }else if($type[$i]==4){
                            $moren=$default[$i]?:'0';
                            $sql.="`".$name[$i]."` decimal(".$length[$i].") DEFAULT '".$moren."' COMMENT '".$beizhux[$i]."',";
                        }else if($type[$i]==5){
                            $moren=NULL;
                            $sql.="`".$name[$i]."` date DEFAULT ".$moren." COMMENT '".$beizhux[$i]."',";
                        }else if($type[$i]==6){
                            $moren=NULL;
                            $sql.="`".$name[$i]."` datetime DEFAULT ".$moren." COMMENT '".$beizhux[$i]."',";
                        }
                    }
                }
                if($table){
                    //$sql.="`merid` int(11) NOT NULL DEFAULT '0',"; //添加的时候会添加 修改忽略
                    $sql.=" PRIMARY KEY (`id`) ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COMMENT='".$beizhu."';";
                    //如果数据库库不存在则创建
                    $isc=Db::query("show tables like '".$table."'");
                    if(!$isc){
                        Db::query($sql);//创建数据库
                    }else{
                        //如果存在则不处理  避免数据丢失
                    }
                }
                
            }else{
                for($i=0;$i<count($name);$i++) {
                    array_push($newarr, array('title' => $name[$i], 'type' => $type[$i], 'name' => $beizhux[$i], 'style' => $style[$i], 'val' => $val[$i],'search'=>$search[$i]));
                }
            }
            //生成php文件
            $this->modelaction($model,$action,$newarr,$title,$table,$daochu,$frontapi);
            //生成前端php文件
            $this->apimodelaction($model,$action,$newarr,$title,$table,$daochu,$frontapi);
  
            $data['status']=1;
            $data['msg']='添加成功';
            return json($data);exit;
        }
        $id=input('param.id/d');
        //查询所有一级菜单
        $navlist=Db::name('escort_admin_permission')->where('parent_id',0)->select()->toArray();
        //查询添加的字段
        $list=Db::name('ohyueo_develo_curdtable')->where('listid',$id)->select()->toArray();
        $name='addcurd';
        $assign=['navlist'=>$navlist,'list'=>$list];
        return View::fetch($name,$assign);
    }
    public function delcurd(){
        $data = array('status' => 0,'msg' => '未知错误');
        $array=input("param.id");
        if(!$array){
            $data['msg']='参数错误';return json($data);exit;
        }
        $arr=explode(",",$array);
        for($i=0;$i<count($arr);$i++){
            if($arr[$i]){
                $list=Db::name('ohyueo_develo_curdlist')->where('id',$arr[$i])->find();
                if($list){
                    $table=$list['table'];
                    if($table){
                        Db::query("DROP TABLE IF EXISTS ".$table);
                    }
                    Db::name('ohyueo_develo_curdlist')->where('id',$arr[$i])->delete();
                    Db::name('ohyueo_develo_curdtable')->where('listid',$arr[$i])->delete();
                    $text = '删除了curd表信息id='.$arr[$i];
                    $this->writeActionLog($text);
                }
            }
        }
        $data['status']=1;
        return json($data);
    }
    protected function modelaction($model,$action,$pararr,$title,$table,$daochu,$frontapi=''){
        //判断是否有 此文件 php文件
        $path=app()->getRootPath().'/app/admin/controller/'.$model.'.php';
        if(!file_exists($path)){ //没有此文件 则生成
            $html="<?php \n/**\n*  * 系统-受国家计算机软件著作权保护 - !\n* =========================================================\n* Copy right 2018-2025 成都海之心科技有限公司, 保留所有权利。\n* ----------------------------------------------\n* 官方网址: http://www.ohyu.cn\n* 这不是一个自由软件！在未得到官方有效许可的前提下禁止对程序代码进行修改和使用。\n* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。\n* =========================================================\n* User: ohyueo\n* Date: ".(date('Y/m/d'))."\n* Time: ".(date('H:i'))."\n*/\nnamespace app\admin\controller;\nuse think\\facade\View;\nuse think\\facade\Db;\nuse app\admin\\traits\AdminAuth;\nuse PhpOffice\PhpSpreadsheet\Spreadsheet;\nuse PhpOffice\PhpSpreadsheet\Writer\Xlsx;\nuse PhpOffice\PhpSpreadsheet\Style\Border;\nuse PhpOffice\PhpSpreadsheet\Style\Fill;";
            //引入模型文件
            $modelname=$this->modelprefix.ucfirst($model).ucfirst($action);
            $html.="\nuse app\admin\models\\".$modelname.";";
            //引入用户模型
            $html.="\nuse app\admin\models\\".$this->modelprefix."UserList;";

            $html.="\nclass ".$model." extends Common{\n\tuse AdminAuth;\n\n ";

            $biao1="\t";$biao2="\t\t";$biao3="\t\t\t";$biao4="\t\t\t\t";$biao5="\t\t\t\t\t";$biao6="\t\t\t\t\t\t";
            //生成列表文件
            $html.=$biao1."public function ".$action."(){\n ";
            //接收dao这个参数
            $html.=$biao2."\$dao=input('param.dao');\n";
            $html.=$biao2."if(request()->isPost() || \$dao==1){\n";
            $html.=$biao3."\$page=input('param.page');\n".$biao3."if(!\$page){\n".$biao4."\$page=1;\n".$biao3."}\n".$biao3."\$limit=input('param.limit');\n".$biao3."if(!\$limit){\n".$biao4."\$limit=10;//每页显示条数\n".$biao3."}\n";
            //搜索数据
            $html.=$biao3."\$where=[];\n";
            $html.=$biao3."\$id=input('param.id');\n".$biao3."if(\$id){\n".$biao4."\$where[]=['id','=',\$id];\n".$biao3."}\n";
            $isuid=0;
            //循环生成搜索条件
            if($pararr){
                for ($i=0;$i<count($pararr);$i++){
                    $fna=$pararr[$i]['title'];
                    $search=$pararr[$i]['search'];
                    if($search=='on' || $search==1){
                        $html.=$biao3.'$'.$fna."=input('param.".$fna."');\n";
                        $html.=$biao3."if(\$".$fna."){\n".$biao4."\$where[]=['".$fna."','like','%'.\$".$fna.".'%'];\n".$biao3."}\n";
                    }
                    if($fna=='uid'){
                        $isuid=1;
                    }
                }
            }
            
            //查询产品
            $html.=$biao3."\$count = ".$modelname."::countData(['where'=>\$where]);\n";
            //如果dao=1则不查询分页数据
            $html.=$biao3."if(\$dao!=1){\n".$biao3."\$data=".$modelname."::findAllData(['where'=>\$where,'limit'=>\$limit,'page'=>\$page,'order'=>'id Desc','field'=>'*','isarr'=>true]);\n".$biao3."}\n";
            //如果dao=1则查询全部数据
            $html.=$biao3."if(\$dao==1){\n".$biao3."\$data=".$modelname."::findAllData(['where'=>\$where,'order'=>'id Desc','field'=>'*','isarr'=>true]);\n".$biao3."}\n";

            //循环data数据
            $html.=$biao3."if(\$data){\n".$biao4."for(\$i=0;\$i<count(\$data);\$i++){\n";
            if($isuid){
                //如果有uid字段则写入上面注释的代码
                $html.=$biao4."\$uid=\$data[\$i]['uid'];\n".$biao4."\$data[\$i]['nick']=".$this->modelprefix."UserList::where('id',\$uid)->value('nick');\n".$biao4."\$headimg=".$this->modelprefix."UserList::where('id',\$uid)->value('headimg');\n".$biao4."\$data[\$i]['headimg']=getFullImageUrl(\$headimg);\n";
            }
            $html.=$biao4."}\n".$biao3."}\n";

            // if($dao==1){
            //     $excel = new \PHPExcel(); //引用phpexcel
            //     $excel->setActiveSheetIndex(0);
            //     $namex = '配餐订单信息';
            //     $excel->getActiveSheet()->setTitle($namex); //设置表名
            //     $excel->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);//所有单元格（列）默认宽度
            //     $excel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(50);
            //     $excel->getDefaultStyle()->getFont()->setName('微软雅黑');//字体
            //     $styleThinBlackBorderOutline = array(
            //         'borders' => array(
            //             'allborders' => array( //设置全部边框
            //                 'style' => \PHPExcel_Style_Border::BORDER_THIN //粗的是thick
            //             ),
            //         ),
            //     );
            //     // 设置单元格的宽度
            //     $excel->getActiveSheet()->getColumnDimension('A')->setWidth(15);#设置单元格宽度
            //     $excel->getActiveSheet()->getColumnDimension('B')->setWidth(30);#设置单元格宽度
            //     $excel->getActiveSheet()->getColumnDimension('C')->setWidth(30);#设置单元格宽度
            //     $excel->getActiveSheet()->getColumnDimension('D')->setWidth(20);#设置单元格宽度
            //     $excel->getActiveSheet()->getColumnDimension('E')->setWidth(20);#设置单元格宽度
            //     $excel->getActiveSheet()->getColumnDimension('F')->setWidth(20);#设置单元格宽度
            //     $excel->getActiveSheet()->getColumnDimension('G')->setWidth(20);#设置单元格宽度
            //     $excel->getActiveSheet()->getColumnDimension('H')->setWidth(20);#设置单元格宽度
            //     $excel->getActiveSheet()->getColumnDimension('I')->setWidth(20);#设置单元格宽度
			// 	$excel->getActiveSheet()->getColumnDimension('J')->setWidth(20);#设置单元格宽度
            //     $cno=count($data)+1;
            //     $excel->getActiveSheet()->getStyle('A1:'.'J'.$cno)->applyFromArray($styleThinBlackBorderOutline);
            //     //设置表头
            //     $excel->setActiveSheetIndex(0)
            //         ->setCellValue('A1', 'ID')
            //         ->setCellValue('B1', '日期')
            //         ->setCellValue('C1', '年费')
            //         ->setCellValue('D1', '季度')
            //         ->setCellValue('E1', '学校')
            //         ->setCellValue('F1', '配餐公司')
            //         ->setCellValue('G1', '份数')
            //         ->setCellValue('H1', '金额')
            //         ->setCellValue('I1', '创建时间')
			// 		->setCellValue('J1', '状态');
            //     $excel->getActiveSheet()->getStyle( 'A1:D1')->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
            //     $excel->getActiveSheet()->getStyle( 'A1:D1')->getFill()->getStartColor()->setARGB('FCE4D6');//设置填充颜色
            //     $excel->getActiveSheet()->getStyle( 'D1:J1')->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
            //     $excel->getActiveSheet()->getStyle( 'D1:J1')->getFill()->getStartColor()->setARGB('FFF2CC');//设置填充颜色
			// 	$zmo=0;
			// 	$fenno=0;
            //     foreach ($data as $k => $v) {
			// 		$zmo+=$v['money'];
			// 		$fenno+=$v['fenno'];
            //         //从第二行开始写入数据（第一行为表头）
            //         $excel->getActiveSheet()->setCellValue('A' . ($k + 2), $v['id']."\t");
            //         $excel->getActiveSheet()->setCellValue('B' . ($k + 2), $v['riqi']);
            //         $excel->getActiveSheet()->setCellValue('C' . ($k + 2), $v['nianfen']);
            //         $excel->getActiveSheet()->setCellValue('D' . ($k + 2), $v['jidu']);
            //         $excel->getActiveSheet()->setCellValue('E' . ($k + 2), $v['schoolname']);
            //         $excel->getActiveSheet()->setCellValue('F' . ($k + 2), $v['merchantname']);
            //         $excel->getActiveSheet()->setCellValue('G' . ($k + 2), $v['fenno']);
            //         $excel->getActiveSheet()->setCellValue('H' . ($k + 2), $v['money']);
			// 		$excel->getActiveSheet()->setCellValue('I' . ($k + 2), $v['addtime']);
            //         $excel->getActiveSheet()->setCellValue('J' . ($k + 2), $v['status']==1?'待配送':'已配送');
            //     }
			// 	//统计份数
			// 	$excel->getActiveSheet()->setCellValue('B' . ($cno + 1), '导出数量：'.count($data));
			// 	$excel->getActiveSheet()->setCellValue('G' . ($cno + 1), '配餐份数：'.$fenno);
            //     $excel->getActiveSheet()->setCellValue('H' . ($cno + 1), '配餐金额：'.$zmo);
            //     ob_end_clean();
            //     header('Content-Type: application/vnd.ms-excel');
            //     header('Content-Disposition: attachment;filename="' . $namex . '.xls"');
            //     header('Cache-Control: max-age=0');
            //     $res_excel = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
            //     $res_excel->save('php://output');exit;
            // }

            //如果dao=1则添加以上注释部分代码
            // $html.=$biao3."if(\$dao==1){\n".$biao4."\$excel = new \PHPExcel(); //引用phpexcel\n".$biao4."\$excel->setActiveSheetIndex(0);\n".$biao4."\$namex = '".$title."';\n".$biao4."\$excel->getActiveSheet()->setTitle(\$namex); //设置表名\n".$biao4."\$excel->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);//所有单元格（列）默认宽度\n".$biao4."\$excel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(50);\n".$biao4."\$excel->getDefaultStyle()->getFont()->setName('微软雅黑');//字体\n".$biao4."\$styleThinBlackBorderOutline = array(\n".$biao4."'borders' => array(\n".$biao4."'allborders' => array( //设置全部边框\n".$biao4."'style' => \\PHPExcel_Style_Border::BORDER_THIN //粗的是thick\n".$biao4.")\n".$biao4."),\n".$biao4.");\n".$biao4."// 设置单元格的宽度\n";
            //     // 设置单元格的宽度 设置统一宽度20
            //     $html.=$biao4."\$excel->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);\n";
            //     //设置表头
            //     $html.=$biao4."\$excel->setActiveSheetIndex(0)\n".$biao4."->setCellValue('A1', 'ID')\n";
            //     for ($i=0;$i<count($pararr);$i++){
            //         $fna=$pararr[$i]['name'];
            //         $html.=$biao4."->setCellValue('".chr(65+$i+1)."1', '".$fna."')\n";
            //     }
            //     $html.=$biao4.";\n".$biao4."\$excel->getActiveSheet()->getStyle( 'A1:".chr(65+count($pararr))."1')->getFill()->setFillType(\\PHPExcel_Style_Fill::FILL_SOLID);\n".$biao4."\$excel->getActiveSheet()->getStyle( 'A1:".chr(65+count($pararr))."1')->getFill()->getStartColor()->setARGB('FCE4D6');//设置填充颜色\n";
            //     $html.=$biao4."\$cno=count(\$data)+1;\n".$biao4."\$excel->getActiveSheet()->getStyle('A1:".chr(65+count($pararr))."'.\$cno)->applyFromArray(\$styleThinBlackBorderOutline);\n";

            //     $html.=$biao4."foreach (\$data as \$k => \$v) {\n";
            //         $html.=$biao4."\$excel->getActiveSheet()->setCellValue('A' . (\$k + 2), \$v['id'].\"\t\");\n";
            //     for ($i=0;$i<count($pararr);$i++){
            //         $fna=$pararr[$i]['title'];
            //         $html.=$biao5."\$excel->getActiveSheet()->setCellValue('".chr(65+$i+1)."' . (\$k + 2), \$v['".$fna."']);\n";
            //     }
            //     $html.=$biao4."}\n";
            //     $html.=$biao4."ob_end_clean();\n".$biao4."header('Content-Type: application/vnd.ms-excel');\n".$biao4."header('Content-Disposition: attachment;filename=\"' . \$namex . '.xls\"');\n".$biao4."header('Cache-Control: max-age=0');\n".$biao4."\$res_excel = PHPExcel_IOFactory::createWriter(\$excel, 'Excel2007');\n".$biao4."\$res_excel->save('php://output');exit;\n";
            // $html.=$biao3."}\n";
            //dao==1结束

            // if($dao==1){
			// 	$spreadsheet = new Spreadsheet(); //引用phpspreadsheet
			// 	$sheet = $spreadsheet->getActiveSheet();
			// 	$namex = '测试旧的';
			// 	$sheet->setTitle($namex); //设置表名
			// 	$sheet->getDefaultColumnDimension()->setWidth(20);//所有单元格（列）默认宽度
			// 	$sheet->getDefaultRowDimension()->setRowHeight(50);
			// 	$spreadsheet->getDefaultStyle()->getFont()->setName('微软雅黑');//字体
			// 	$styleThinBlackBorderOutline = [
			// 		'borders' => [
			// 			'allBorders' => [
			// 				'borderStyle' => Border::BORDER_THIN,
			// 			],
			// 		],
			// 	];
			// 	// 设置单元格的宽度
			// 	$sheet->getDefaultColumnDimension()->setWidth(20);
			// 	$sheet->setCellValue('A1', 'ID')
			// 		->setCellValue('B1', '姓名')
			// 		->setCellValue('C1', '用户');
			// 	$sheet->getStyle( 'A1:C1')->getFill()->setFillType(Fill::FILL_SOLID);
			// 	$sheet->getStyle( 'A1:C1')->getFill()->getStartColor()->setARGB('FCE4D6');//设置填充颜色
				
			// 	$cno=count($data)+1;
			// 	$sheet->getStyle('A1:C'.$cno)->applyFromArray($styleThinBlackBorderOutline);
			// 	foreach ($data as $k => $v) {
			// 		$sheet->setCellValue('A' . ($k + 2), $v['id']."\t");
			// 		$sheet->setCellValue('B' . ($k + 2), $v['姓名']);
			// 		$sheet->setCellValue('C' . ($k + 2), $v['领队手机号']);
			// 	}
			// 	ob_end_clean();
			// 	header('Content-Type: application/vnd.ms-excel');
			// 	header('Content-Disposition: attachment;filename="' . $namex . '.xls"');
			// 	header('Cache-Control: max-age=0');
			// 	$writer = new Xlsx($spreadsheet);
    		// 	$writer->save('php://output');exit;
			// }

            //需要根据最开始dao==1的逻辑修改为 以上注释的逻辑
            $html.=$biao2."if(\$dao==1){\n";
                $html.=$biao3."\$spreadsheet = new Spreadsheet(); //引用phpspreadsheet\n";
                $html.=$biao3."\$sheet = \$spreadsheet->getActiveSheet();\n".$biao3."\$namex = '".$title."';\n".$biao3."\$sheet->setTitle(\$namex); //设置表名\n".$biao3."\$sheet->getDefaultColumnDimension()->setWidth(20);//所有单元格（列）默认宽度\n";
                $html.=$biao3."\$sheet->getDefaultRowDimension()->setRowHeight(50);\n".$biao3."\$spreadsheet->getDefaultStyle()->getFont()->setName('微软雅黑');//字体\n";
                $html.=$biao3."\$styleThinBlackBorderOutline = [\n".$biao3."'borders' => [\n".$biao3."'allBorders' => [\n";
                $html.=$biao3."'borderStyle' => Border::BORDER_THIN,\n";
                $html.=$biao3."],\n";
                $html.=$biao3."],\n";
                $html.=$biao3."];\n";
                $html.=$biao3."// 设置单元格的宽度\n".$biao3."\$sheet->getDefaultColumnDimension()->setWidth(20);\n";
            //设置表头
            $html.=$biao3."\$sheet->setCellValue('A1', 'ID')\n";
            for ($i=0;$i<count($pararr);$i++){
                $fna=$pararr[$i]['name'];
                $html.=$biao4."->setCellValue('".chr(65+$i+1)."1', '".$fna."')\n";
            }
            $html.=$biao3.";\n".$biao3."\$sheet->getStyle( 'A1:".chr(65+count($pararr))."1')->getFill()->setFillType(Fill::FILL_SOLID);\n".$biao3."\$sheet->getStyle( 'A1:".chr(65+count($pararr))."1')->getFill()->getStartColor()->setARGB('FCE4D6');//设置填充颜色\n";
            $html.=$biao3."\$cno=count(\$data)+1;\n".$biao3."\$sheet->getStyle('A1:".chr(65+count($pararr))."'.\$cno)->applyFromArray(\$styleThinBlackBorderOutline);\n";
            $html.=$biao3."foreach (\$data as \$k => \$v) {\n";
            $html.=$biao4."\$sheet->setCellValue('A' . (\$k + 2), \$v['id'].\"\t\");\n";
            for ($i=0;$i<count($pararr);$i++){
                $fna=$pararr[$i]['title'];
                $html.=$biao5."\$sheet->setCellValue('".chr(65+$i+1)."' . (\$k + 2), \$v['".$fna."']);\n";
            }
            $html.=$biao3."}\n";
            $html.=$biao3."ob_end_clean();\n".$biao3."header('Content-Type: application/vnd.ms-excel');\n".$biao3."header('Content-Disposition: attachment;filename=\"' . \$namex . '.xls\"');\n".$biao3."header('Cache-Control: max-age=0');\n".$biao3."\$writer = new Xlsx(\$spreadsheet);\n".$biao3."\$writer->save('php://output');exit;\n";
            $html.=$biao2."}\n";
            //dao==1结束
            

            //返回数据
            $html.=$biao3."\$res = [\"data\" => \$data,\"code\"=>0,'message'=>'请求成功','count'=>\$count];\n".$biao3."return json(\$res);\n";
            $html.=$biao2."}\n";
            //权限
            $html.=$biao2."\$permis = \$this->getPermissions('".$model."/".$action."');\n".$biao2."View::assign('data', \$this->actions);\n".$biao2."View::assign('permis', \$permis);\n";
            //显示页面
            $html.=$biao2."return View::fetch();\n";
            $html.=$biao1."}\n";

            //生成添加文件
            $html.=$biao1."public function add".$action."(){\n ";
            $html.=$biao2."if(request()->isPost()){\n";
            $html.=$biao3."\$data = array('status' => 0, 'msg' => '未知错误');\n";
            if($pararr){
                $arrtit=$biao3."\$arr=array(\n";
                for ($i=0;$i<count($pararr);$i++){
                    $style=$pararr[$i]['style'];
                    $fna=$pararr[$i]['title'];
                    //如果是富文本编辑器  则接收fna2
                    if($style==4){
                        $html.=$biao3.'$'.$fna."=input('param.".$fna."2','','remove_xss');\n";
                    }else{
                        //如果字段是addtime
                        if($fna=='addtime'){
                            $html.=$biao3.'$'.$fna."=gettime();\n";
                        }else{
                            $html.=$biao3.'$'.$fna."=input('param.".$fna."');\n";
                        }
                    }
                    $arrtit.=$biao4."'".$fna."'=>$".$fna.",\n";
                }
                $arrtit.=$biao3.");";
                $html.=$arrtit."\n";
            }
            $html.=$biao3."\$id=".$modelname."::addData(\$arr);\n".$biao3."\$id=\$id->id;\n".$biao3."\$text = '添加了".$title."-id='.\$id;\n".$biao3."\$this->writeActionLog(\$text);\n".$biao3."\$data['status'] = 1;\n".$biao3."return json(\$data);exit;\n";
            $html.=$biao2."}\n";
            //显示页面
            $html.=$biao2."return View::fetch('edit".$action."');\n";
            $html.=$biao1."}\n";

            //生成修改文件
            $html.=$biao1."public function edit".$action."(){\n ";
            $html.=$biao2."if(request()->isPost()){\n";
            $html.=$biao3."\$data = array('status' => 0, 'msg' => '未知错误');\n";
            if($pararr){
                $arrtit=$biao3."\$arr=array(\n";
                for ($i=0;$i<count($pararr);$i++){
                    $style=$pararr[$i]['style'];
                    $fna=$pararr[$i]['title'];
                    //如果是富文本编辑器  则接收fna2
                    if($style==4){
                        $html.=$biao3.'$'.$fna."=input('param.".$fna."2','','remove_xss');\n";
                    }else{
                        //如果字段是addtime
                        if($fna=='addtime'){
                            $html.=$biao3.'$'.$fna."=gettime();\n";
                        }else{
                            $html.=$biao3.'$'.$fna."=input('param.".$fna."');\n";
                        }
                    }
                    $arrtit.=$biao4."'".$fna."'=>$".$fna.",\n";
                }
                $arrtit.=$biao3.");";
                $html.=$arrtit."\n";
            }
            $html.=$biao3."\$id = input('param.id');\n".$biao3."if (\$id){\n";
            $html.=$biao4.$modelname."::updatesData(['where'=>['id'=>\$id],'data'=>\$arr]);\n".$biao4."\$text = '修改了".$title."-id='.\$id;\n".$biao4."\$this->writeActionLog(\$text);\n".$biao4."\$data['status'] = 1;\n".$biao4."return json(\$data);exit;\n";
            $html.=$biao3."}\n";
            $html.=$biao2."}\n";
            //显示页面
            $html.=$biao2."return View::fetch('edit".$action."');\n";
            $html.=$biao1."}\n";

            //生成删除页面
            $html.=$biao1."public function del".$action."(){\n ";
            $html.=$biao2."\$data = array('status' => 0,'msg' => '未知错误');\n".$biao2."\$array=input(\"param.id\");\n".$biao2."if(!\$array){\n".$biao3."\$data['msg']='参数错误';return json(\$data);exit;\n".$biao2."}\n";
            $html.=$biao2."\$arr=explode(\",\",\$array);\n".$biao2."for(\$i=0;\$i<count(\$arr);\$i++){\n";
            $html.=$biao3."if(\$arr[\$i]){\n".$biao4."".$modelname."::deleteData(['where'=>['id'=>\$arr[\$i]]]);\n".$biao4."\$text = '删除了".$title."id='.\$arr[\$i].'的数据';\n".$biao4."\$this->writeActionLog(\$text);\n".$biao3."}\n";
            $html.=$biao2."}\n".$biao2."\$data['status']=1;\n".$biao2."return json(\$data);\n";
            $html.=$biao1."}\n";

            $html.="\n} ";
            file_put_contents($path,$html);

            /***********添加权限************/
            //在admin/traits/AdminAuth.php文件中的$actions数组里面添加权限,分别是：add方法名=>add方法名,edit方法名=>edit方法名,del方法名=>del方法名
            $authcode="\n\t\t'add".$action."'=>'add".$action."','edit".$action."'=>'edit".$action."','del".$action."'=>'del".$action."',";
            $authpath=app()->getRootPath().'/app/admin/traits/AdminAuth.php';
            //查询$path文件的内容 是否有$authcode里面的这个字符串
            $authfile=file_get_contents($authpath);
            if(strpos($authfile,$authcode)===false){
                //查找$action = [在文件中的行数
                $lineNumber = 0;
                $lines = file($authpath);
                foreach ($lines as $lineNumber => $line) {
                    if (strpos($line, '$actions = [') !== false) {
                        break;
                    }
                }
                // 读取文件内容到数组中
                $files = file($authpath);
                // 在特定的行插入新的内容
                array_splice($files, $lineNumber +2, 0, $authcode);
                // 把数组的内容写回到文件中
                file_put_contents($authpath, $files);
            }
            //在admin/traits/AdminAuth.php文件中的$action数组里面添加权限
            $authcode="\n\t\t'".$action."',";
            $authpath=app()->getRootPath().'/app/admin/traits/AdminAuth.php';
            //查询$path文件的内容 是否有$authcode里面的这个字符串
            $authfile=file_get_contents($authpath);
            if(strpos($authfile,$authcode)===false){
                //查找$action = [在文件中的行数
                $lineNumber = 0;
                $lines = file($authpath);
                foreach ($lines as $lineNumber => $line) {
                    if (strpos($line, '$action = [') !== false) {
                        break;
                    }
                }
                // 读取文件内容到数组中
                $files = file($authpath);
                // 在特定的行插入新的内容
                array_splice($files, $lineNumber +2, 0, $authcode);
                // 把数组的内容写回到文件中
                file_put_contents($authpath, $files);
            }
            /***********添加权限 end************/


            //在生成一个模型文件
            $modelname=$this->modelprefix.ucfirst($model).ucfirst($action);
            $path=app()->getRootPath().'/app/api/models/'.$modelname.'.php';
            $html1="<?php \n/**\n*  * 系统-受国家计算机软件著作权保护 - !\n* =========================================================\n* Copy right 2018-2025 成都海之心科技有限公司, 保留所有权利。\n* ----------------------------------------------\n* 官方网址: http://www.ohyu.cn\n* 这不是一个自由软件！在未得到官方有效许可的前提下禁止对程序代码进行修改和使用。\n* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。\n* =========================================================\n* User: ohyueo\n* Date: ".(date('Y/m/d'))."\n* Time: ".(date('H:i'))."\n*/\nnamespace app\api\models;\n;
            \nclass ".$modelname." extends BaseModel{\n\n ";
            $html1.="\n} ";
            file_put_contents($path,$html1);

            //在admin模块下的models文件夹下生成一个模型文件
            $path=app()->getRootPath().'/app/admin/models/'.$modelname.'.php';
            $html1="<?php \n/**\n*  * 系统-受国家计算机软件著作权保护 - !\n* =========================================================\n* Copy right 2018-2025 成都海之心科技有限公司, 保留所有权利。\n* ----------------------------------------------\n* 官方网址: http://www.ohyu.cn\n* 这不是一个自由软件！在未得到官方有效许可的前提下禁止对程序代码进行修改和使用。\n* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。\n* =========================================================\n* User: ohyueo\n* Date: ".(date('Y/m/d'))."\n* Time: ".(date('H:i'))."\n*/\nnamespace app\admin\models;\n;
            \nclass ".$modelname." extends BaseModel{\n\n ";
            $html1.="\n} ";
            file_put_contents($path,$html1);
        }else{ //如果已经有这个文件了  则直接在倒数第二行插入代码
            $modelname=$this->modelprefix.ucfirst($model).ucfirst($action);

            $html="";
            $biao1="\t";$biao2="\t\t";$biao3="\t\t\t";$biao4="\t\t\t\t";$biao5="\t\t\t\t\t";$biao6="\t\t\t\t\t\t";
            //生成列表文件
            $html.=$biao1."public function ".$action."(){\n ";
            //接收dao这个参数
            $html.=$biao2."\$dao=input('param.dao');\n";
            $html.=$biao2."if(request()->isPost() || \$dao==1){\n";
            $html.=$biao3."\$page=input('param.page');\n".$biao3."if(!\$page){\n".$biao4."\$page=1;\n".$biao3."}\n".$biao3."\$limit=input('param.limit');\n".$biao3."if(!\$limit){\n".$biao4."\$limit=10;//每页显示条数\n".$biao3."}\n";
            $isuid=0;
            //搜索数据
            $html.=$biao3."\$where=[];\n";
            $html.=$biao3."\$id=input('param.id');\n".$biao3."if(\$id){\n".$biao4."\$where[]=['id','=',\$id];\n".$biao3."}\n";
            //循环生成搜索条件
            if($pararr){
                for ($i=0;$i<count($pararr);$i++){
                    $fna=$pararr[$i]['title'];
                    $search=$pararr[$i]['search'];
                    if($search=='on' || $search==1){
                        $html.=$biao3.'$'.$fna."=input('param.".$fna."');\n";
                        $html.=$biao3."if(\$".$fna."){\n".$biao4."\$where[]=['".$fna."','like','%'.\$".$fna.".'%'];\n".$biao3."}\n";
                    }
                    if($fna=='uid'){
                        $isuid=1;
                    }
                }
            }
            //查询产品
            $html.=$biao3."\$count = ".$modelname."::countData(['where'=>\$where]);\n";
            //如果dao=1则不查询分页数据
            $html.=$biao3."if(\$dao!=1){\n".$biao4."\$data=".$modelname."::findAllData(['where'=>\$where,'limit'=>\$limit,'page'=>\$page,'order'=>'id Desc','field'=>'*','isarr'=>true]);\n".$biao3."}\n";
            //如果dao=1则查询全部数据
            $html.=$biao3."if(\$dao==1){\n".$biao4."\$data=".$modelname."::findAllData(['where'=>\$where,'order'=>'id Desc','field'=>'*','isarr'=>true]);\n".$biao3."}\n";
            //$html.=$biao3."\$data=Db::name('".$table."')->where(\$where)->page(\$page,\$limit)->order('id desc')->select()->toArray();\n";
            //循环data数据
            $html.=$biao3."if(\$data){\n".$biao4."for(\$i=0;\$i<count(\$data);\$i++){\n";
            if($isuid){
                //如果有uid字段则写入上面注释的代码
                $html.=$biao4."\$uid=\$data[\$i]['uid'];\n".$biao4."\$data[\$i]['nick']=".$this->modelprefix."UserList::where('id',\$uid)->value('nick');\n".$biao4."\$headimg=".$this->modelprefix."UserList::where('id',\$uid)->value('headimg');\n".$biao4."\$data[\$i]['headimg']=getFullImageUrl(\$headimg);\n";
            }
            $html.=$biao4."}\n".$biao3."}\n";

            //如果dao=1则添加以上注释部分代码
            // $html.=$biao3."if(\$dao==1){\n".$biao4."\$excel = new \PHPExcel(); //引用phpexcel\n".$biao4."\$excel->setActiveSheetIndex(0);\n".$biao4."\$namex = '".$title."';\n".$biao4."\$excel->getActiveSheet()->setTitle(\$namex); //设置表名\n".$biao4."\$excel->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);//所有单元格（列）默认宽度\n".$biao4."\$excel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(50);\n".$biao4."\$excel->getDefaultStyle()->getFont()->setName('微软雅黑');//字体\n".$biao4."\$styleThinBlackBorderOutline = array(\n".$biao4."'borders' => array(\n".$biao4."'allborders' => array( //设置全部边框\n".$biao4."'style' => \\PHPExcel_Style_Border::BORDER_THIN //粗的是thick\n".$biao4.")\n".$biao4."),\n".$biao4.");\n".$biao4."// 设置单元格的宽度\n";
            //     // 设置单元格的宽度 设置统一宽度20
            //     $html.=$biao4."\$excel->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);\n";
            //     //设置表头
            //     $html.=$biao4."\$excel->setActiveSheetIndex(0)\n".$biao4."->setCellValue('A1', 'ID')\n";
            //     for ($i=0;$i<count($pararr);$i++){
            //         $fna=$pararr[$i]['name'];
            //         $html.=$biao4."->setCellValue('".chr(65+$i+1)."1', '".$fna."')\n";
            //     }
            //     $html.=$biao4.";\n".$biao4."\$excel->getActiveSheet()->getStyle( 'A1:".chr(65+count($pararr))."1')->getFill()->setFillType(\\PHPExcel_Style_Fill::FILL_SOLID);\n".$biao4."\$excel->getActiveSheet()->getStyle( 'A1:".chr(65+count($pararr))."1')->getFill()->getStartColor()->setARGB('FCE4D6');//设置填充颜色\n";
            //     $html.=$biao4."\$cno=count(\$data)+1;\n".$biao4."\$excel->getActiveSheet()->getStyle('A1:".chr(65+count($pararr))."'.\$cno)->applyFromArray(\$styleThinBlackBorderOutline);\n";
            //     $html.=$biao4."foreach (\$data as \$k => \$v) {\n";
            //         $html.=$biao4."\$excel->getActiveSheet()->setCellValue('A' . (\$k + 2), \$v['id'].\"\t\");\n";
            //     for ($i=0;$i<count($pararr);$i++){
            //         $fna=$pararr[$i]['title'];
            //         $html.=$biao5."\$excel->getActiveSheet()->setCellValue('".chr(65+$i+1)."' . (\$k + 2), \$v['".$fna."']);\n";
            //     }
            //     $html.=$biao4."}\n";
            //     $html.=$biao4."ob_end_clean();\n".$biao4."header('Content-Type: application/vnd.ms-excel');\n".$biao4."header('Content-Disposition: attachment;filename=\"' . \$namex . '.xls\"');\n".$biao4."header('Cache-Control: max-age=0');\n".$biao4."\$res_excel = PHPExcel_IOFactory::createWriter(\$excel, 'Excel2007');\n".$biao4."\$res_excel->save('php://output');exit;\n";
            // $html.=$biao3."}\n";
            //dao==1结束

            // if($dao==1){
			// 	$spreadsheet = new Spreadsheet(); //引用phpspreadsheet
			// 	$sheet = $spreadsheet->getActiveSheet();
			// 	$namex = '测试旧的';
			// 	$sheet->setTitle($namex); //设置表名
			// 	$sheet->getDefaultColumnDimension()->setWidth(20);//所有单元格（列）默认宽度
			// 	$sheet->getDefaultRowDimension()->setRowHeight(50);
			// 	$spreadsheet->getDefaultStyle()->getFont()->setName('微软雅黑');//字体
			// 	$styleThinBlackBorderOutline = [
			// 		'borders' => [
			// 			'allBorders' => [
			// 				'borderStyle' => Border::BORDER_THIN,
			// 			],
			// 		],
			// 	];
			// 	// 设置单元格的宽度
			// 	$sheet->getDefaultColumnDimension()->setWidth(20);
			// 	$sheet->setCellValue('A1', 'ID')
			// 		->setCellValue('B1', '姓名')
			// 		->setCellValue('C1', '用户');
			// 	$sheet->getStyle( 'A1:C1')->getFill()->setFillType(Fill::FILL_SOLID);
			// 	$sheet->getStyle( 'A1:C1')->getFill()->getStartColor()->setARGB('FCE4D6');//设置填充颜色
				
			// 	$cno=count($data)+1;
			// 	$sheet->getStyle('A1:C'.$cno)->applyFromArray($styleThinBlackBorderOutline);
			// 	foreach ($data as $k => $v) {
			// 		$sheet->setCellValue('A' . ($k + 2), $v['id']."\t");
			// 		$sheet->setCellValue('B' . ($k + 2), $v['姓名']);
			// 		$sheet->setCellValue('C' . ($k + 2), $v['领队手机号']);
			// 	}
			// 	ob_end_clean();
			// 	header('Content-Type: application/vnd.ms-excel');
			// 	header('Content-Disposition: attachment;filename="' . $namex . '.xls"');
			// 	header('Cache-Control: max-age=0');
			// 	$writer = new Xlsx($spreadsheet);
    		// 	$writer->save('php://output');exit;
			// }

            //需要根据最开始dao==1的逻辑修改为 以上注释的逻辑
            $html.=$biao2."if(\$dao==1){\n";
                $html.=$biao3."\$spreadsheet = new Spreadsheet(); //引用phpspreadsheet\n";
                $html.=$biao3."\$sheet = \$spreadsheet->getActiveSheet();\n".$biao3."\$namex = '".$title."';\n".$biao3."\$sheet->setTitle(\$namex); //设置表名\n".$biao3."\$sheet->getDefaultColumnDimension()->setWidth(20);//所有单元格（列）默认宽度\n";
                $html.=$biao3."\$sheet->getDefaultRowDimension()->setRowHeight(50);\n".$biao3."\$spreadsheet->getDefaultStyle()->getFont()->setName('微软雅黑');//字体\n";
                $html.=$biao3."\$styleThinBlackBorderOutline = [\n".$biao3."'borders' => [\n".$biao3."'allBorders' => [\n";
                $html.=$biao3."'borderStyle' => Border::BORDER_THIN,\n";
                $html.=$biao3."],\n";
                $html.=$biao3."],\n";
                $html.=$biao3."];\n";
                $html.=$biao3."// 设置单元格的宽度\n".$biao3."\$sheet->getDefaultColumnDimension()->setWidth(20);\n";
            //设置表头
            $html.=$biao3."\$sheet->setCellValue('A1', 'ID')\n";
            for ($i=0;$i<count($pararr);$i++){
                $fna=$pararr[$i]['name'];
                $html.=$biao4."->setCellValue('".chr(65+$i+1)."1', '".$fna."')\n";
            }
            $html.=$biao3.";\n".$biao3."\$sheet->getStyle( 'A1:".chr(65+count($pararr))."1')->getFill()->setFillType(Fill::FILL_SOLID);\n".$biao3."\$sheet->getStyle( 'A1:".chr(65+count($pararr))."1')->getFill()->getStartColor()->setARGB('FCE4D6');//设置填充颜色\n";
            $html.=$biao3."\$cno=count(\$data)+1;\n".$biao3."\$sheet->getStyle('A1:".chr(65+count($pararr))."'.\$cno)->applyFromArray(\$styleThinBlackBorderOutline);\n";
            $html.=$biao3."foreach (\$data as \$k => \$v) {\n";
            $html.=$biao4."\$sheet->setCellValue('A' . (\$k + 2), \$v['id'].\"\t\");\n";
            for ($i=0;$i<count($pararr);$i++){
                $fna=$pararr[$i]['title'];
                $html.=$biao5."\$sheet->setCellValue('".chr(65+$i+1)."' . (\$k + 2), \$v['".$fna."']);\n";
            }
            $html.=$biao3."}\n";
            $html.=$biao3."ob_end_clean();\n".$biao3."header('Content-Type: application/vnd.ms-excel');\n".$biao3."header('Content-Disposition: attachment;filename=\"' . \$namex . '.xls\"');\n".$biao3."header('Cache-Control: max-age=0');\n".$biao3."\$writer = new Xlsx(\$spreadsheet);\n".$biao3."\$writer->save('php://output');exit;\n";
            $html.=$biao2."}\n";
            //dao==1结束



            //返回数据
            $html.=$biao3."\$res = [\"data\" => \$data,\"code\"=>0,'message'=>'请求成功','count'=>\$count];\n".$biao3."return json(\$res);\n";
            $html.=$biao2."}\n";
            //权限
            $html.=$biao2."\$permis = \$this->getPermissions('".$model."/".$action."');\n".$biao2."View::assign('data', \$this->actions);\n".$biao2."View::assign('permis', \$permis);\n";
            //显示页面
            $html.=$biao2."return View::fetch();\n";
            $html.=$biao1."}\n";

            //生成添加文件
            $html.=$biao1."public function add".$action."(){\n ";
            $html.=$biao2."if(request()->isPost()){\n";
            $html.=$biao3."\$data = array('status' => 0, 'msg' => '未知错误');\n";
            if($pararr){
                $arrtit=$biao3."\$arr=array(\n";
                for ($i=0;$i<count($pararr);$i++){
                    $style=$pararr[$i]['style'];
                    $fna=$pararr[$i]['title'];
                    //如果是富文本编辑器  则接收fna2
                    if($style==4){
                        $html.=$biao3.'$'.$fna."=input('param.".$fna."2','','remove_xss');\n";
                    }else{
                        $html.=$biao3.'$'.$fna."=input('param.".$fna."');\n";
                    }
                    $arrtit.=$biao4."'".$fna."'=>$".$fna.",\n";
                }
                $arrtit.=$biao3.");";
                $html.=$arrtit."\n";
            }
            $html.=$biao3."\$id=".$modelname."::addData(\$arr);\n".$biao3."\$id=\$id->id;\n".$biao3."\$text = '添加了".$title."-id='.\$id;\n".$biao3."\$this->writeActionLog(\$text);\n".$biao3."\$data['status'] = 1;\n".$biao3."return json(\$data);exit;\n";
            $html.=$biao2."}\n";
            //显示页面
            $html.=$biao2."return View::fetch('edit".$action."');\n";
            $html.=$biao1."}\n";

            //生成修改文件
            $html.=$biao1."public function edit".$action."(){\n ";
            $html.=$biao2."if(request()->isPost()){\n";
            $html.=$biao3."\$data = array('status' => 0, 'msg' => '未知错误');\n";
            if($pararr){
                $arrtit=$biao3."\$arr=array(\n";
                for ($i=0;$i<count($pararr);$i++){
                    $style=$pararr[$i]['style'];
                    $fna=$pararr[$i]['title'];
                    //如果是富文本编辑器  则接收fna2
                    if($style==4){
                        $html.=$biao3.'$'.$fna."=input('param.".$fna."2','','remove_xss');\n";
                    }else{
                        $html.=$biao3.'$'.$fna."=input('param.".$fna."');\n";
                    }
                    $arrtit.=$biao4."'".$fna."'=>$".$fna.",\n";
                }
                $arrtit.=$biao3.");";
                $html.=$arrtit."\n";
            }
            $html.=$biao3."\$id = input('param.id');\n".$biao3."if (\$id){\n";
            $html.=$biao4.$modelname."::updatesData(['where'=>['id'=>\$id],'data'=>\$arr]);\n".$biao4."\$text = '修改了".$title."-id='.\$id;\n".$biao4."\$this->writeActionLog(\$text);\n".$biao4."\$data['status'] = 1;\n".$biao4."return json(\$data);exit;\n";
            $html.=$biao3."}\n";
            $html.=$biao2."}\n";
            //显示页面
            $html.=$biao2."return View::fetch('edit".$action."');\n";
            $html.=$biao1."}\n";

            //生成删除页面
            $html.=$biao1."public function del".$action."(){\n ";
            $html.=$biao2."\$data = array('status' => 0,'msg' => '未知错误');\n".$biao2."\$array=input(\"param.id\");\n".$biao2."if(!\$array){\n".$biao3."\$data['msg']='参数错误';return json(\$data);exit;\n".$biao2."}\n";
            $html.=$biao2."\$arr=explode(\",\",\$array);\n".$biao2."for(\$i=0;\$i<count(\$arr);\$i++){\n";
            $html.=$biao3."if(\$arr[\$i]){\n".$biao4."".$modelname."::deleteData(['where'=>['id'=>\$arr[\$i]]]);\n".$biao4."\$text = '删除了".$title."id='.\$arr[\$i].'的数据';\n".$biao4."\$this->writeActionLog(\$text);\n".$biao3."}\n";
            $html.=$biao2."}\n".$biao2."\$data['status']=1;\n".$biao2."return json(\$data);\n";
            $html.=$biao1."}\n";

            //在文件末尾的第二行添加代码
            $handle = fopen($path, 'r+');
            $i = -1;
            $lastLine = '';
            while(true){
                fseek($handle, $i, SEEK_END);
                $char = fgetc($handle);
                if($char == "\n"){
                    fwrite($handle, $html. $lastLine);
                    break;
                }else{
                    $lastLine .= $char;
                }
                $i --;
            }
            $hval="\nuse app\admin\models\\".$modelname.";";
            //查询$path文件的内容 是否有$hval里面的这个字符串
            $file=file_get_contents($path);
            if(strpos($file,$hval)===false){
                //在文件的第十九行插入代码
                $lineNumber = 19; // 要插入的行号
                // 读取文件内容到数组中
                $files = file($path);
                // 在特定的行插入新的内容
                array_splice($files, $lineNumber - 1, 0, $hval);
                // 把数组的内容写回到文件中
                file_put_contents($path, $files);
            }
            //file_put_contents($path,$html);


            /***********添加权限************/
            //在admin/traits/AdminAuth.php文件中的$actions数组里面添加权限,分别是：add方法名=>add方法名,edit方法名=>edit方法名,del方法名=>del方法名
            $authcode="\n\t\t'add".$action."'=>'add".$action."','edit".$action."'=>'edit".$action."','del".$action."'=>'del".$action."',";
            $authpath=app()->getRootPath().'/app/admin/traits/AdminAuth.php';
            //查询$path文件的内容 是否有$authcode里面的这个字符串
            $authfile=file_get_contents($authpath);
            if(strpos($authfile,$authcode)===false){
                //查找$action = [在文件中的行数
                $lineNumber = 0;
                $lines = file($authpath);
                foreach ($lines as $lineNumber => $line) {
                    if (strpos($line, '$actions = [') !== false) {
                        break;
                    }
                }
                // 读取文件内容到数组中
                $files = file($authpath);
                // 在特定的行插入新的内容
                array_splice($files, $lineNumber +2, 0, $authcode);
                // 把数组的内容写回到文件中
                file_put_contents($authpath, $files);
            }
            //在admin/traits/AdminAuth.php文件中的$action数组里面添加权限
            $authcode="\n\t\t'".$action."',";
            $authpath=app()->getRootPath().'/app/admin/traits/AdminAuth.php';
            //查询$path文件的内容 是否有$authcode里面的这个字符串
            $authfile=file_get_contents($authpath);
            if(strpos($authfile,$authcode)===false){
                //查找$action = [在文件中的行数
                $lineNumber = 0;
                $lines = file($authpath);
                foreach ($lines as $lineNumber => $line) {
                    if (strpos($line, '$action = [') !== false) {
                        break;
                    }
                }
                // 读取文件内容到数组中
                $files = file($authpath);
                // 在特定的行插入新的内容
                array_splice($files, $lineNumber +2, 0, $authcode);
                // 把数组的内容写回到文件中
                file_put_contents($authpath, $files);
            }
            /***********添加权限 end************/

            //在生成一个模型文件
            $modelname=$this->modelprefix.ucfirst($model).ucfirst($action);
            $path=app()->getRootPath().'/app/api/models/'.$modelname.'.php';
            $html1="<?php \n/**\n*  * 系统-受国家计算机软件著作权保护 - !\n* =========================================================\n* Copy right 2018-2025 成都海之心科技有限公司, 保留所有权利。\n* ----------------------------------------------\n* 官方网址: http://www.ohyu.cn\n* 这不是一个自由软件！在未得到官方有效许可的前提下禁止对程序代码进行修改和使用。\n* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。\n* =========================================================\n* User: ohyueo\n* Date: ".(date('Y/m/d'))."\n* Time: ".(date('H:i'))."\n*/\nnamespace app\api\models;\n;
            \nclass ".$modelname." extends BaseModel{\n\n ";
            $html1.="\n} ";
            file_put_contents($path,$html1);
            //在admin模块下的models文件夹下生成一个模型文件
            $path=app()->getRootPath().'/app/admin/models/'.$modelname.'.php';
            $html1="<?php \n/**\n*  * 系统-受国家计算机软件著作权保护 - !\n* =========================================================\n* Copy right 2018-2025 成都海之心科技有限公司, 保留所有权利。\n* ----------------------------------------------\n* 官方网址: http://www.ohyu.cn\n* 这不是一个自由软件！在未得到官方有效许可的前提下禁止对程序代码进行修改和使用。\n* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。\n* =========================================================\n* User: ohyueo\n* Date: ".(date('Y/m/d'))."\n* Time: ".(date('H:i'))."\n*/\nnamespace app\admin\models;\n;
            \nclass ".$modelname." extends BaseModel{\n\n ";
            $html1.="\n} ";
            file_put_contents($path,$html1);
        }

        //生成静态文件
        $this->generatehtml($model,$action,$pararr,$title,$table,$daochu,$frontapi);
    }
    /**
     * 生成html文件
     **/
    protected function generatehtml($model,$action,$pararr,$title,$table,$daochu,$frontapi){
        //先判断列表文件
        $models=strtolower($model);
        $path2=app()->getRootPath().'/app/admin/view/'.$models.'/'.$action.'.html';
        $mk=app()->getRootPath().'/app/admin/view/'.$models;
        if(!file_exists($path2)){
            if(!is_dir($mk)){
                mkdir(iconv("UTF-8", "GBK", $mk),0777,true);
            }
            //顶部代码
            $html="<!DOCTYPE html>\n<html>\n<head>\n\t<meta charset=\"utf-8\">\n\t<title>管理</title>\n\t<meta name=\"renderer\" content=\"webkit\">\n\t<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\">\n\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0\">\n\t<link rel=\"stylesheet\" href=\"/admin/layuiadmin/layui/css/layui.css\" media=\"all\">\n\t<link rel=\"stylesheet\" href=\"/admin/layuiadmin/style/admin.css\" media=\"all\">\n\t<style>\n\t\t@font-face {\n\t\t\tfont-family: 'iconfont';  \n\t\t\tsrc: url('https://at.alicdn.com/t/font_2565954_q2snf28joy.woff2?t=1621766073287') format('woff2'),\n\t\t\turl('https://at.alicdn.com/t/font_2565954_q2snf28joy.woff?t=1621766073287') format('woff'),\n\t\t\turl('https://at.alicdn.com/t/font_2565954_q2snf28joy.ttf?t=1621766073287') format('truetype');\n\t\t}\n\t\t.iconfont{\n\t\t\tfont-family:\"iconfont\" !important;\n\t\t\tfont-size:16px;font-style:normal;\n\t\t\t-webkit-font-smoothing: antialiased;\n\t\t\t-webkit-text-stroke-width: 0.2px;\n\t\t\t-moz-osx-font-smoothing: grayscale;}\n\t\t.layui-layer-admin .layui-layer-ico {\n\t\t\tbackground: url(\"/admin/layuiadmin/gb.png\") no-repeat!important;\n\t\t\tbackground-size:16px 16px!important;\n\t\t}\n\t</style>\n</head>\n<body>\n";
            //中间代码
            $html.="<div class=\"layui-fluid\">\n\t<div class=\"layui-card\">\n\t\t<div class=\"layui-form layui-card-header layuiadmin-card-header-auto\">\n\t\t\t<div class=\"layui-form-item\">\n";
            $html.="\t\t\t\t<div class=\"layui-inline\">\n\t\t\t\t\t<label class=\"layui-form-label\">ID</label>\n\t\t\t\t\t<div class=\"layui-input-block\">\n\t\t\t\t\t\t<input type=\"text\" name=\"id\" placeholder=\"请输入\" autocomplete=\"off\" class=\"layui-input\">\n\t\t\t\t\t</div>\n\t\t\t\t</div>\n";
            $isuid=0;
            //循环搜索条件
            if($pararr){
                for ($i=0;$i<count($pararr);$i++){
                    $fna=$pararr[$i]['title'];
                    $search=$pararr[$i]['search'];
                    $name=$pararr[$i]['name'];
                    if($search=='on' || $search==1){
                        if($fna=='status'){
                            $html.="\t\t\t\t<div class=\"layui-inline\">\n\t\t\t\t\t<label class=\"layui-form-label\">".$name."</label>\n\t\t\t\t\t<div class=\"layui-input-block\">\n\t\t\t\t\t\t<select name=\"".$fna."\">\n\t\t\t\t\t\t\t<option value=\"\">请选择</option>\n\t\t\t\t\t\t\t<option value=\"1\">上线</option>\n\t\t\t\t\t\t\t<option value=\"2\">下线</option>\n\t\t\t\t\t\t</select>\n\t\t\t\t\t</div>\n\t\t\t\t</div>\n";
                        }else{
                            //如果字段是addtime 那么跳出当前循环执行下一个循环
                            if($fna=='addtime'){
                                continue;
                            }
                            //如果是用户uid则除了input外还需要显示 选择用户
                            if($fna=='uid'){
                                $isuid=1;
                                $html.="\t\t\t\t<div class=\"layui-inline\">\n\t\t\t\t\t<label class=\"layui-form-label\">".$name."</label>\n\t\t\t\t\t<div class=\"layui-input-inline\">\n\t\t\t\t\t\t<input type=\"text\" name=\"".$fna."\" id=\"".$fna."\" placeholder=\"请输入\" autocomplete=\"off\" class=\"layui-input\">\n\t\t\t\t\t</div>\n\t\t\t\t\t<div class=\"layui-form-mid layui-word-aux chaxun\" data-type=\"chaxun\">\n\t\t\t\t\t\t<span style=\"color: #1E9FFF;cursor: pointer;\">选择用户</span>\n\t\t\t\t\t</div>\n\t\t\t\t</div>\n";
                            }else{
                                $html.="\t\t\t\t<div class=\"layui-inline\">\n\t\t\t\t\t<label class=\"layui-form-label\">".$name."</label>\n\t\t\t\t\t<div class=\"layui-input-block\">\n\t\t\t\t\t\t<input type=\"text\" name=\"".$fna."\" placeholder=\"请输入\" autocomplete=\"off\" class=\"layui-input\">\n\t\t\t\t\t</div>\n\t\t\t\t</div>\n";
                            }
                        }
                    }
                }
            }
            $html.="\t\t\t\t<div class=\"layui-inline\">\n\t\t\t\t\t<button class=\"layui-btn layuiadmin-btn-useradmin\" lay-submit lay-filter=\"LAY-user-front-search\">\n\t\t\t\t\t\t<i class=\"layui-icon layui-icon-search layuiadmin-button-btn\"></i>\n\t\t\t\t\t</button>\n";
            //如果需要导出则增加导出按钮
            if($daochu==1){
                $html.="\t\t\t\t\t<button class=\"layui-btn layuiadmin-btn-useradmin\" lay-submit lay-filter=\"order-daochu\">\n\t\t\t\t\t\t导出数据\n\t\t\t\t\t</button>\n";
            }
            $html.="\t\t\t\t</div>\n\t\t\t</div>\n\t\t</div>";
            //内容部分
            $html.="\n\t\t<div class=\"layui-card-body\">";
            $html.="\n\t\t\t<table class=\"layui-hide\" id=\"LAY-user-managet\" lay-filter=\"LAY-user-managet\"></table>";
            //上面操作按钮
            $html.="\n\t\t\t<script type=\"text/html\" id=\"demo-toolbar-setRowChecked\">";
            $html.="\n\t\t\t\t<div class=\"layui-btn-container\">";
            $html.="\n\t\t\t\t\t{in name=\"\$data.add".$action."\" value=\"\$permis\"}";
            $html.="\n\t\t\t\t\t<button class=\"layui-btn layui-btn\" lay-event=\"add\">添加</button>";
            $html.="\n\t\t\t\t\t{/in}";
            $html.="\n\t\t\t\t\t{in name=\"\$data.del".$action."\" value=\"\$permis\"}";
            $html.="\n\t\t\t\t\t<button class=\"layui-btn layui-btn\" lay-event=\"del\">删除</button>";
            $html.="\n\t\t\t\t\t{/in}";
            $html.="\n\t\t\t\t</div>";
            $html.="\n\t\t\t</script>";
            //$html.="\n\t\t<div class=\"layui-card-body\">\n\t\t\t<div style=\"padding-bottom: 10px;\">\n\t\t\t{in name=\"\$data.add".$action."\" value=\"\$permis\"}\n\t\t\t\t<button class=\"layui-btn layuiadmin-btn-useradmin\" data-type=\"add\">添加</button>\n\t\t\t{/in}\n\t\t\t{in name=\"\$data.del".$action."\" value=\"\$permis\"}\n\t\t\t\t<button class=\"layui-btn layuiadmin-btn-useradmin\" data-type=\"del\">删除</button>\n\t\t\t{/in}\n\t\t\t</div>\n\t\t\t<table class=\"layui-table\" lay-data=\"{ url:'/admin/".$model."/".$action."',method:'post', page:true, id:'LAY-user-managet'}\" lay-filter=\"LAY-user-managet\">\n\t\t\t<thead>\n\t\t\t\t<tr>\n";

            //$html.="\t\t\t\t\t<th lay-data=\"{type: 'checkbox', fixed: 'left'}\"></th>\n";
            //$html.="\t\t\t\t\t<th lay-data=\"{field:'id', width:110, sort: true}\">ID</th>\n";
            $isimg=false;//是否有图片
            if($pararr){
                for ($i=0;$i<count($pararr);$i++){
                    $style=$pararr[$i]['style'];
                    $titlex=$pararr[$i]['title'];
                    $name=$pararr[$i]['name'];
                    if($style==3){
                        $isimg=true;
                        //如果有图片则显示图片
                        if($isimg){
                            $html.="\n\t\t\t<script type=\"text/html\" id=\"".$titlex."Demo\">\n\t\t\t\t<img src=\"{{d.".$titlex."}}\" onclick=\"openimg('{{d.".$titlex."}}')\" style=\"height: 30px;\">\n\t\t\t</script>\n";
                        }
                        //$html.="\t\t\t\t\t<th lay-data=\"{field:'".$titlex."', templet: '#imgDemo'}\">".$name."</th>\n";
                    }else{
                        //$html.="\t\t\t\t\t<th lay-data=\"{field:'".$titlex."'}\">".$name."</th>\n";
                    }
                }
            }
            //$html.="\t\t\t\t\t<th lay-data=\"{fixed: 'right', width:220, align:'center', toolbar: '#barDemo'}\">操作</th>\n\t\t\t\t</tr>\n\t\t\t</thead>\n\t\t\t</table>\n";
            
            //如果有用户uid则显示用户信息
            if($isuid==1){
                $html.="\n\t\t\t<script type=\"text/html\" id=\"user\">\n\t\t\t\t{{# if(d.uid>0){ }}\n\t\t\t\t<span style=\"color: #FF6A00;cursor:pointer\" lay-href=\"/admin/User/userinfo?id={{d.uid}}\" title=\"用户信息\">\n\t\t\t\t  {{d.nick}}\n\t\t\t\t  <i class=\"layui-icon layui-icon-search\"></i>\n\t\t\t\t</span>\n\t\t\t\t{{# }else{ }}\n\t\t\t\t未绑定\n\t\t\t\t{{# } }}\n\t\t\t</script>\n";
            }
            $html.="\t\t\t<script type=\"text/html\" id=\"barDemo\">\n\t\t\t{in name=\"\$data.edit".$action."\" value=\"\$permis\"}\n\t\t\t\t<a class=\"layui-btn layui-btn-normal layui-btn-xs\" lay-event=\"edit\"><i class=\"layui-icon layui-icon-edit\"></i>编辑</a>\n\t\t\t{/in}\n\t\t\t{in name=\"\$data.del".$action."\" value=\"\$permis\"}\n\t\t\t\t<a class=\"layui-btn layui-btn-danger layui-btn-xs\" lay-event=\"del\"><i class=\"layui-icon layui-icon-delete\"></i>删除</a>\n\t\t\t{/in}\n\t\t\t</script>";
            $html.="\n\t\t</div>\n\t</div>\n</div>\n";
            //底部js部分
            $html.="<script src=\"/admin/layuiadmin/layui/layui.js\"></script>\n";
            $html.="<script src=\"/admin/js/public.js\"></script>\n<script>\n";
            $html.="\tlayui.config({\n\t\tbase: '/admin/layuiadmin/' \n\t}).extend({\n\t\tindex: 'lib/index' \n\t}).use(['index', 'useradmin','form', 'table'], function(){\n\t\tvar $ = layui.$,form = layui.form,table = layui.table;\n";
            
            $html.="\t\ttable.render({\n\t\t\telem: '#LAY-user-managet'\n\t\t\t,url: '/admin/".$model."/".$action."'\n\t\t\t,cols: [[\n";
            $html.="\t\t\t\t{type: 'checkbox', fixed: 'left'}\n";
            $html.="\t\t\t\t,{field:'id', width:110, sort: true, title: 'ID'}\n";
            if($pararr){
                for ($i=0;$i<count($pararr);$i++){
                    $style=$pararr[$i]['style'];
                    $titlex=$pararr[$i]['title'];
                    $name=$pararr[$i]['name'];
                    if($style==3){
                        $html.="\t\t\t\t,{field:'".$titlex."', templet: '#".$titlex."Demo', title: '".$name."'}\n";
                    }else if($style==2){
                        $html.="\t\t\t\t,{field:'".$titlex."', title: '".$name."', templet: function(d){\n\t\t\t\t\tif(d.".$titlex." === 1){\n\t\t\t\t\t\treturn '<span style=\"color: #16baaa\">上线</span>';\n\t\t\t\t\t} else {\n\t\t\t\t\t\treturn '<span style=\"color: #666666\">下线</span>';\n\t\t\t\t\t}\n\t\t\t\t}}\n";
                    }else{
                        //如果有用户uid则显示用户信息
                        if($titlex=='uid'){
                            $html.="\t\t\t\t,{field:'".$titlex."', templet: '#user', title: '".$name."'}\n";
                        }else{
                            $html.="\t\t\t\t,{field:'".$titlex."', title: '".$name."'}\n";
                        }
                    }
                }
            }
            $html.="\t\t\t\t,{fixed: 'right', width:220, align:'center',title:'操作', toolbar: '#barDemo'}\n";
            $html.="\t\t\t]]\n\t\t\t,page: true\n\t\t\t,method:'post'\n\t\t\t,toolbar: '#demo-toolbar-setRowChecked'\n\t\t});\n";
            
            
            /**搜索**/
            $html.="\t\tform.on('submit(LAY-user-front-search)', function(data){\n\t\t\tvar field = data.field;\n\t\t\t$.post(\"{:url('admin/".$model."/".$action."')}\", field, function(data,state){});\n\t\t\t//执行重载\n\t\t\ttable.reload('LAY-user-managet', {\n\t\t\t\twhere: field\n\t\t\t});\n\t\t});\n";
            //如果需要导出则增加导出
            if($daochu==1){
                $html.="\t\tform.on('submit(order-daochu)', function(data){\n\t\t\tvar field = data.field;\n\t\t\tvar par=toQueryString(field);\n\t\t\twindow.location.href='/admin/".$model."/".$action."?dao=1&'+par;\n\t\t});\n";
            }
            /**操作按钮**/
            $html.="\t\ttable.on('tool(LAY-user-managet)', function(obj){\n\t\t\tvar data = obj.data; //获得当前行数据\n\t\t\tvar layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）\n\t\t\tvar tr = obj.tr; //获得当前行 tr 的DOM对象\n";
            $html.="\t\t\tif(layEvent === 'del'){ //删除\n\t\t\t\tlayer.confirm('真的删除当前数据吗', function(index){\n\t\t\t\t\tobj.del(); //删除对应行（tr）的DOM结构，并更新缓存\n\t\t\t\t\tlayer.close(index);\n\t\t\t\t\tvar dataid=data['id'];\n\t\t\t\t\t//向服务端发送删除指令\n\t\t\t\t\t$.post(\n\t\t\t\t\t\t\"{:url('admin/".$model."/del".$action."')}\",\n\t\t\t\t\t\t{\"id\":dataid},\n\t\t\t\t\t\tfunction(data,state){\n\t\t\t\t\t\t\tif(state != \"success\"){\n\t\t\t\t\t\t\t\tlayer.msg(\"请求出错!\");\n\t\t\t\t\t\t\t}else if(data.status == 1){\n\t\t\t\t\t\t\t\ttable.reload('LAY-user-managet');\n\t\t\t\t\t\t\t\tlayer.msg('已删除');\n\t\t\t\t\t\t\t}else{\n\t\t\t\t\t\t\t\tlayer.msg(data.msg);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t);\n\t\t\t\t});\n\t\t\t}\n";
            /**修改**/
            $html.="\t\t\telse if(layEvent === 'edit'){\n\t\t\t\tvar id=data['id'];\n\t\t\t\tlayer.open({\n\t\t\t\t\ttype: 2\n\t\t\t\t\t,title: '修改".$title."'\n\t\t\t\t\t,content: 'edit".$action.".html'\n\t\t\t\t\t,maxmin: true\n\t\t\t\t\t,area: ['80%', '80%']\n\t\t\t\t\t,btn: ['确定', '取消']\n\t\t\t\t\t,success: function(layero,index){\n\t\t\t\t\t\tvar body = layui.layer.getChildFrame('body', index);\n\t\t\t\t\t\tbody.find(\"#id\").val(id);\n";
            if($pararr) {
                for ($i=0;$i<count($pararr);$i++) {
                    $titlex=$pararr[$i]['title'];//字段名
                    $style=$pararr[$i]['style'];
                    $val=$pararr[$i]['val'];//表单值
                    if($style==1) { //文本框
                        //如果字段是addtime 那么跳出当前循环执行下一个循环
                        if($titlex=='addtime'){
                            continue;
                        }
                        $html .= "\t\t\t\t\t\tbody.find(\"#" . $titlex . "\").val(data['" . $titlex . "']);\n";
                    }else if($style==2){ //单选框
                        $valarr=explode(',',$val);
                        if($valarr && count($valarr)>0){
                            for ($d=0;$d<count($valarr);$d++){
                                $html.="\t\t\t\t\t\tbody.find(\"input[name=" . $titlex . "][value=".$valarr[$d]."]\").attr(\"checked\", data['" . $titlex . "'] == ".$valarr[$d]." ? true : false);\n";
                            }
                        }else{
                            $html.="\t\t\t\t\t\tbody.find(\"input[name=" . $titlex . "][value=".$val."]\").attr(\"checked\", data['" . $titlex . "'] == ".$val." ? true : false);\n";
                        }
                    }else if($style==3) { //上传图片
                        $html .= "\t\t\t\t\t\tbody.find(\"#" . $titlex . "\").val(data['" . $titlex . "']);\n";
                        //除了赋值图片 还需要显示图片
                        $html .= "\t\t\t\t\t\tvar imgurl=data['".$titlex."'];\n\t\t\t\t\t\tif(imgurl){\n\t\t\t\t\t\t\tbody.find(\"#".$titlex."\").next().attr(\"src\",imgurl);\n\t\t\t\t\t\t}\n";
                    }else if($style==4) { //富文本
                        $html .= "\t\t\t\t\t\tbody.find(\"#" . $titlex . "\").val(data['" . $titlex . "']);\n";
                        //添加以上代码
                        $html .= "\t\t\t\t\t\tbody.find(\"#" . $titlex . "2\").val(data['" . $titlex . "']);\n";
                    }
                }
            }
            $html.="\t\t\t\t\t\tform.render();\n\t\t\t\t\t}\n\t\t\t\t\t,yes: function(index, layero){\n\t\t\t\t\t\tvar iframeWindow = window['layui-layer-iframe'+ index],submitID = 'LAY-user-front-submit',submit = layero.find('iframe').contents().find('#'+ submitID);\n\t\t\t\t\t\t//监听提交\n\t\t\t\t\t\tiframeWindow.layui.form.on('submit('+ submitID +')', function(data){\n\t\t\t\t\t\t\tvar field = data.field; //获取提交的字段\n\t\t\t\t\t\t\t//提交 Ajax 成功后，静态更新表格中的数据\n\t\t\t\t\t\t\t//$.ajax({});\n\t\t\t\t\t\t\t$.post(\n\t\t\t\t\t\t\t\t\"{:url('/admin/".$model."/edit".$action."')}\",\n\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\tfunction(data,state){\n\t\t\t\t\t\t\t\t\tif(state != \"success\"){\n\t\t\t\t\t\t\t\t\t\tlayer.msg(\"请求出错!\");\n\t\t\t\t\t\t\t\t\t}else if(data.status == 1){\n\t\t\t\t\t\t\t\t\t\tlayer.msg('修改成功');\n\t\t\t\t\t\t\t\t\t\ttable.reload('LAY-user-managet'); //数据刷新\n\t\t\t\t\t\t\t\t\t}else{\n\t\t\t\t\t\t\t\t\t\tlayer.msg(data.msg);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tlayer.close(index); //关闭弹层\n\t\t\t\t\t\t});\n\t\t\t\t\t\tsubmit.trigger('click');\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n";
            $html.="\t\t});\n";
            /**操作按钮结束**/

            //事件部分重新写  工具栏事件
            $html.="\n\t\ttable.on('toolbar(LAY-user-managet)', function(obj){\n\t\t\tvar checkStatus = table.checkStatus(obj.config.id); //获取选中行状态";
                $html.="\n\t\t\tswitch(obj.event){";
                    $html.="\n\t\t\t\tcase 'add':";
                        $html.="\n\t\t\t\t\tlayer.open({";
                            $html.="\n\t\t\t\t\t\ttype: 2";
                            $html.="\n\t\t\t\t\t\t,title: '添加".$title."'";
                            $html.="\n\t\t\t\t\t\t,content: 'add".$action.".html'";
                            $html.="\n\t\t\t\t\t\t,maxmin: true";
                            $html.="\n\t\t\t\t\t\t,area: ['80%', '80%']";
                            $html.="\n\t\t\t\t\t\t,btn: ['确定', '取消']";
                            $html.="\n\t\t\t\t\t\t,yes: function(index, layero){";
                                $html.="\n\t\t\t\t\t\t\tvar iframeWindow = window['layui-layer-iframe'+ index],submitID = 'LAY-user-front-submit',submit = layero.find('iframe').contents().find('#'+ submitID);";
                                $html.="\n\t\t\t\t\t\t\t//监听提交";
                                $html.="\n\t\t\t\t\t\t\tiframeWindow.layui.form.on('submit('+ submitID +')', function(data){";
                                    $html.="\n\t\t\t\t\t\t\t\tvar field = data.field; //获取提交的字段";
                                    $html.="\n\t\t\t\t\t\t\t\t//提交 Ajax 成功后，静态更新表格中的数据";
                                    $html.="\n\t\t\t\t\t\t\t\t//$.ajax({});";
                                    $html.="\n\t\t\t\t\t\t\t\t$.post(";
                                        $html.="\n\t\t\t\t\t\t\t\t\t\"{:url('/admin/".$model."/add".$action."')}\",";
                                        $html.="\n\t\t\t\t\t\t\t\t\tfield,";
                                        $html.="\n\t\t\t\t\t\t\t\t\tfunction(data,state){";
                                            $html.="\n\t\t\t\t\t\t\t\t\t\tif(state != \"success\"){";
                                                $html.="\n\t\t\t\t\t\t\t\t\tlayer.msg(\"请求出错!\");";
                                            $html.="\n\t\t\t\t\t\t\t\t\t}else if(data.status == 1){";
                                                $html.="\n\t\t\t\t\t\t\t\t\tlayer.msg('添加成功');";
                                                $html.="\n\t\t\t\t\t\t\t\t\ttable.reload('LAY-user-managet'); //数据刷新";
                                            $html.="\n\t\t\t\t\t\t\t\t\t}else{";
                                                $html.="\n\t\t\t\t\t\t\t\t\tlayer.msg(data.msg);";
                                            $html.="\n\t\t\t\t\t\t\t\t\t}";
                                        $html.="\n\t\t\t\t\t\t\t\t}";
                                    $html.="\n\t\t\t\t\t\t\t);";
                                    $html.="\n\t\t\t\t\t\t\tlayer.close(index); //关闭弹层";
                                $html.="\n\t\t\t\t\t\t});";
                                $html.="\n\t\t\t\t\t\tsubmit.trigger('click');";
                            $html.="\n\t\t\t\t\t}";
                        $html.="\n\t\t\t\t});";
                    $html.="\n\t\t\t\tbreak;";
                    $html.="\n\t\t\t\tcase 'del':";
                        $html.="\n\t\t\t\t\tvar dataid='';";
                        $html.="\n\t\t\t\t\tfor(var i=0;i<checkStatus.data.length;i++){";
                            $html.="\n\t\t\t\t\t\tdataid=dataid+','+checkStatus.data[i]['id'];";
                        $html.="\n\t\t\t\t\t}";
                        $html.="\n\t\t\t\t\tlayer.confirm('确定删除选中数据吗？', function(index) {";
                            $html.="\n\t\t\t\t\t\t$.post(";
                                $html.="\n\t\t\t\t\t\t\t\"{:url('/admin/".$model."/del".$action."')}\",";
                                $html.="\n\t\t\t\t\t\t\t{\"id\":dataid},";
                                $html.="\n\t\t\t\t\t\t\tfunction(data,state){";
                                    $html.="\n\t\t\t\t\t\t\t\tif(state != \"success\"){";
                                        $html.="\n\t\t\t\t\t\t\t\t\tlayer.msg(\"请求出错!\");";
                                    $html.="\n\t\t\t\t\t\t\t\t}else if(data.status == 1){";
                                        $html.="\n\t\t\t\t\t\t\t\t\ttable.reload('LAY-user-managet');";
                                        $html.="\n\t\t\t\t\t\t\t\t\tlayer.msg('已删除');";
                                    $html.="\n\t\t\t\t\t\t\t\t}else{";
                                        $html.="\n\t\t\t\t\t\t\t\t\tlayer.msg(data.msg);";
                                    $html.="\n\t\t\t\t\t\t\t\t}";
                                $html.="\n\t\t\t\t\t\t\t}";
                            $html.="\n\t\t\t\t\t\t);";
                        $html.="\n\t\t\t\t\t});";
                    $html.="\n\t\t\t\tbreak;";
                $html.="\n\t\t\t}";
            $html.="\n\t\t});";

            //如果有uid则需要选择用户
            // var active = {
            //     chaxun:function(){
            //         layer.open({
            //             type: 2
            //             ,title: '搜索用户'
            //             ,content: '/admin/User/sousuo.html'
            //             ,maxmin: true
            //             ,area: ['700px', '450px']
            //             ,btn: ['确定', '取消']
            //             ,success: function(layero,index){
            //                 var body = layui.layer.getChildFrame('body', index);
            //                 form.render();
            //             }
            //             ,yes: function(index, layero){
            //                 var iframeWindow = window['layui-layer-iframe'+ index]
            //                     ,submitID = 'LAY-user-front-submit'
            //                     ,submit = layero.find('iframe').contents().find('#'+ submitID);
            //                 //监听提交
            //                 iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
            //                     var field = data.field; //获取提交的字段
            //                     var uid=field.uids;
            //                     $('#uid').val(uid);
            //                     layer.close(index); //关闭弹层
            //                 });
            //                 submit.trigger('click');
            //             }
            //         });
            //     }
            // }
            // $('.chaxun').on('click', function(){
            //     var type = $(this).data('type');
            //     active[type] ? active[type].call(this) : '';
            // });
            //如果有uid字段则添加上面注释的这一段代码
            if($isuid){
                $html.="\n\t\tvar active = {";
                $html.="\n\t\t\tchaxun:function(){";
                $html.="\n\t\t\t\tlayer.open({";
                $html.="\n\t\t\t\t\ttype: 2";
                $html.="\n\t\t\t\t\t,title: '搜索用户'";
                $html.="\n\t\t\t\t\t,content: '/admin/User/sousuo.html'";
                $html.="\n\t\t\t\t\t,maxmin: true";
                $html.="\n\t\t\t\t\t,area: ['700px', '450px']";
                $html.="\n\t\t\t\t\t,btn: ['确定', '取消']";
                $html.="\n\t\t\t\t\t,success: function(layero,index){";
                $html.="\n\t\t\t\t\t\tvar body = layui.layer.getChildFrame('body', index);";
                $html.="\n\t\t\t\t\t\tform.render();";
                $html.="\n\t\t\t\t\t}";
                $html.="\n\t\t\t\t\t,yes: function(index, layero){";
                $html.="\n\t\t\t\t\t\tvar iframeWindow = window['layui-layer-iframe'+ index]";
                $html.="\n\t\t\t\t\t\t\t,submitID = 'LAY-user-front-submit'";
                $html.="\n\t\t\t\t\t\t\t,submit = layero.find('iframe').contents().find('#'+ submitID);";
                $html.="\n\t\t\t\t\t\t//监听提交";
                $html.="\n\t\t\t\t\t\tiframeWindow.layui.form.on('submit('+ submitID +')', function(data){";
                $html.="\n\t\t\t\t\t\t\tvar field = data.field; //获取提交的字段";
                $html.="\n\t\t\t\t\t\t\tvar uid=field.uids;";
                $html.="\n\t\t\t\t\t\t\t$('#uid').val(uid);";
                $html.="\n\t\t\t\t\t\t\tlayer.close(index); //关闭弹层";
                $html.="\n\t\t\t\t\t\t});";
                $html.="\n\t\t\t\t\t\tsubmit.trigger('click');";
                $html.="\n\t\t\t\t\t}";
                $html.="\n\t\t\t\t});";
                $html.="\n\t\t\t}";
                $html.="\n\t\t}";
                $html.="\n\t\t$('.chaxun').on('click', function(){";
                $html.="\n\t\t\tvar type = $(this).data('type');";
                $html.="\n\t\t\tactive[type] ? active[type].call(this) : '';";
                $html.="\n\t\t});";
            }

            $html.="\n\t});\n";
            
            /**事件**/
            //$html.="\t\tvar active = {\n";
            /**添加**/
            //$html.="\t\t\tadd:function(){\n\t\t\t\tlayer.open({\n\t\t\t\t\ttype: 2\n\t\t\t\t\t,title: '添加".$title."'\n\t\t\t\t\t,content: 'add".$action.".html'\n\t\t\t\t\t,maxmin: true\n\t\t\t\t\t,area: ['80%', '80%']\n\t\t\t\t\t,btn: ['确定', '取消']\n\t\t\t\t\t,yes: function(index, layero){\n\t\t\t\t\t\tvar iframeWindow = window['layui-layer-iframe'+ index]\n\t\t\t\t\t\t\t,submitID = 'LAY-user-front-submit'\n\t\t\t\t\t\t\t,submit = layero.find('iframe').contents().find('#'+ submitID);\n\t\t\t\t\t\t//监听提交\n\t\t\t\t\t\tiframeWindow.layui.form.on('submit('+ submitID +')', function(data){\n\t\t\t\t\t\t\tvar field = data.field; //获取提交的字段\n\t\t\t\t\t\t\t//提交 Ajax 成功后，静态更新表格中的数据\n\t\t\t\t\t\t\t//$.ajax({});\n\t\t\t\t\t\t\t$.post(\n\t\t\t\t\t\t\t\t\"{:url('/admin/".$model."/add".$action."')}\",\n\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\tfunction(data,state){\n\t\t\t\t\t\t\t\t\tif(state != \"success\"){\n\t\t\t\t\t\t\t\t\t\tlayer.msg(\"请求出错!\");\n\t\t\t\t\t\t\t\t\t}else if(data.status == 1){\n\t\t\t\t\t\t\t\t\t\tlayer.msg('添加成功');\n\t\t\t\t\t\t\t\t\t\ttable.reload('LAY-user-managet'); //数据刷新\n\t\t\t\t\t\t\t\t\t}else{\n\t\t\t\t\t\t\t\t\t\tlayer.msg(data.msg);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tlayer.close(index); //关闭弹层\n\t\t\t\t\t\t});\n\t\t\t\t\t\tsubmit.trigger('click');\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t},\n";
            /**删除**/
            //$html.="\t\t\tdel: function(){\n\t\t\t\tvar checkStatus = table.checkStatus('LAY-user-managet'),checkData = checkStatus.data; //得到选中的数据\n\t\t\t\tif(checkData.length === 0){\n\t\t\t\t\treturn layer.msg('请选择数据');\n\t\t\t\t}\n\t\t\t\tvar dataid='';\n\t\t\t\tfor(var i=0;i<checkData.length;i++){\n\t\t\t\t\tdataid=dataid+','+checkData[i]['id'];\n\t\t\t\t}\n\t\t\t\tlayer.confirm('确定删除选中数据吗？', function(index) {\n\t\t\t\t\t$.post(\n\t\t\t\t\t\t\"{:url('/admin/".$model."/del".$action."')}\",\n\t\t\t\t\t\t{\"id\":dataid},\n\t\t\t\t\t\tfunction(data,state){\n\t\t\t\t\t\t\tif(state != \"success\"){\n\t\t\t\t\t\t\t\tlayer.msg(\"请求出错!\");\n\t\t\t\t\t\t\t}else if(data.status == 1){\n\t\t\t\t\t\t\t\ttable.reload('LAY-user-managet');\n\t\t\t\t\t\t\t\tlayer.msg('已删除');\n\t\t\t\t\t\t\t}else{\n\t\t\t\t\t\t\t\tlayer.msg(data.msg);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t);\n\t\t\t\t});\n\t\t\t}\n";
            //$html.="\t\t}\n";
            /**事件结束**/
            //$html.="\t\t$('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){\n\t\t\tvar type = $(this).data('type');\n\t\t\tactive[type] ? active[type].call(this) : '';\n\t\t});\n";
            //$html.="\t});\n";
            //如果有图片则添加预览图片的openimg方法
            if($isimg){
                $html.="\tfunction openimg(img){\n\t\tlayer.open({\n\t\t\ttype: 1,\n\t\t\ttitle: false,\n\t\t\tcloseBtn: 0,\n\t\t\tarea: '516px',\n\t\t\tskin: 'layui-layer-nobg', //没有背景色\n\t\t\tshadeClose: true,\n\t\t\tcontent: '<img src=\"'+img+'\" style=\"width:100%;\" />'\n\t\t});\n\t}\n";
            }
            $html.="</script>\n";
            $html.="</body>\n</html>";
            file_put_contents($path2,$html);
        }

        //生成修改文件
        $path3=app()->getRootPath().'/app/admin/view/'.$model.'/edit'.$action.'.html';
        $mk=app()->getRootPath().'/app/admin/view/'.$model;
        if(!file_exists($path3)) {
            if (!is_dir($mk)) {
                mkdir(iconv("UTF-8", "GBK", $mk), 0777, true);
            }
            //是否需要上传图片
            $isimg=false;
            //是否设置了富文本
            $iscontent=false;
            $fuid='';
            /**顶部代码**/
            $html="<!DOCTYPE html>\n<html>\n<head>\n\t<meta charset=\"utf-8\">\n\t<title>管理</title>\n\t<meta name=\"renderer\" content=\"webkit\">\n\t<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\">\n\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0\">\n\t<link rel=\"stylesheet\" href=\"/admin/layuiadmin/layui/css/layui.css\" media=\"all\">\n\t<link rel=\"stylesheet\" href=\"/admin/css/u-ui.css\" media=\"all\">\n</head>\n<body>\n";
            /**中间部分代码**/
            $html.="<div class=\"layui-form\" lay-filter=\"layuiadmin-form-useradmin\" id=\"layuiadmin-form-useradmin\" style=\"padding: 20px 0 0 0;\">\n\t<input type=\"hidden\" name=\"id\" id=\"id\" value=\"\" />\n\t";
            if($pararr) {
                for ($i=0;$i<count($pararr);$i++) {
                    $name=$pararr[$i]['name'];
                    $titlex=$pararr[$i]['title'];//字段名
                    $style=$pararr[$i]['style'];
                    $val=$pararr[$i]['val'];//表单值
                    if($style==1){ //文本框
                        //如果字段是addtime 那么跳出当前循环执行下一个循环
                        if($name=='addtime'){
                            continue;
                        }
                        //如果有uid则显示用户信息
                        if($titlex=='uid'){
                            $isuid=true;
                            $html.="<div class=\"layui-form-item\">\n\t\t<label class=\"layui-form-label\">".$name."</label>\n\t\t<div class=\"layui-input-inline\">\n\t\t\t<input type=\"text\" name=\"uid\" id=\"uid\" placeholder=\"用户ID\" autocomplete=\"off\" class=\"layui-input\">\n\t\t</div>\n\t\t<div class=\"layui-form-mid layui-word-aux chaxun\" data-type=\"chaxun\">\n\t\t\t<span style=\"color: #1E9FFF;cursor: pointer;\">选择用户</span>\n\t\t</div>\n\t</div>\n\t";
                        }else{
                            $html.="<div class=\"layui-form-item\">\n\t\t<label class=\"layui-form-label\">".$name."</label>\n\t\t<div class=\"layui-input-inline\">\n\t\t\t<input type=\"text\" name=\"".$titlex."\" id=\"".$titlex."\" lay-verify=\"required\"  placeholder=\"".$name."\" autocomplete=\"off\" class=\"layui-input\">\n\t\t</div>\n\t</div>\n\t";
                        }
                    }else if($style==2){ //单选框
                        $valarr=explode(',',$val);
                        if($valarr && count($valarr)>0){
                            $html.="<div class=\"layui-form-item\" lay-filter=\"status\">\n\t\t<label class=\"layui-form-label\">".$name."</label>\n\t\t<div class=\"layui-input-block\">\n";
                            for ($d=0;$d<count($valarr);$d++){
                                if($d==0){
                                    $html.="\t\t\t<input type=\"radio\" name=\"".$titlex."\" checked  id=\"".$titlex."\" value=\"".$valarr[$d]."\" title=\"上线\">\n";
                                }else{
                                    $html.="\t\t\t<input type=\"radio\" name=\"".$titlex."\" id=\"".$titlex."\" value=\"".$valarr[$d]."\" title=\"下线\">\n";
                                }
                            }
                            $html.="\t\t</div>\n\t</div>\n\t";
                        }else{
                            $html.="<div class=\"layui-form-item\" lay-filter=\"status\">\n\t\t<label class=\"layui-form-label\">".$name."</label>\n\t\t<div class=\"layui-input-block\">\n";
                            $html.="\t\t\t<input type=\"radio\" name=\"".$titlex."\" checked  id=\"".$titlex."\" value=\"".$val."\" title=\"上线\">\n";
                            $html.="\t\t</div>\n\t</div>\n\t";
                        }
                    }else if($style==3){ //上传图片
                        $isimg=true;
                        $html.="<div class=\"layui-form-item\">\n\t\t<label class=\"layui-form-label\">".$name."</label>\n\t\t<div class=\"layui-input-inline\">\n\t\t\t<input type=\"text\" name=\"".$titlex."\" id=\"".$titlex."\" lay-verify=\"required\"  placeholder=\"".$name."\" autocomplete=\"off\" class=\"layui-input\">\n\t\t\t<img style=\"width:100px;\">\n\t\t</div>\n\t\t<div class=\"layui-input-inline\">\n\t\t\t<button type=\"button\" class=\"layui-btn\" id=\"".$titlex."btn\">上传图片</button>\n\t\t</div>\n\t</div>\n\t";
                    }else if($style==4){ //富文本
                        $iscontent=true;
                        $fuid=$titlex;
                        //添加以上代码
                        $html.="<div class=\"layui-form-item\">\n\t\t<label class=\"layui-form-label\">".$name."</label>\n\t\t<div class=\"layui-input-block\" style=\"width: 80%;height: 400px;\">\n\t\t\t<textarea id=\"".$titlex."\" name=\"".$titlex."\" style=\"height: 400px;\"></textarea>\n\t\t\t<textarea id=\"".$titlex."2\" name=\"".$titlex."2\" style=\"display: none;\"></textarea>\n\t\t</div>\n\t</div>\n\t";
                        //$html.="<div class=\"layui-form-item\">\n\t\t<label class=\"layui-form-label\">".$name."</label>\n\t\t<div class=\"layui-input-block\" style=\"width: 80%;\">\n\t\t\t<textarea name=\"".$titlex."\" id=\"".$titlex."\" lay-verify=\"content\" style=\"display: none;\" class=\"layui-textarea\"></textarea>\n\t\t</div>\n\t</div>\n\t";
                    }
                    
                }
            }
            $html.="\n\t<div class=\"layui-form-item layui-hide\">\n\t\t<input type=\"button\" lay-submit lay-filter=\"LAY-user-front-submit\" id=\"LAY-user-front-submit\" value=\"确认\">\n\t</div>\n</div>\n";
            
            /*如果是富文本那么这个js不一样  废弃*/
            if($iscontent && 1==2){
                $html.="<script src=\"/admin/layuiadmin/layui/Layui-KnifeZ/layui.js\"></script>\n<script src=\"/admin/layuiadmin/layui/ace/ace.js\"></script>\n<script>\n";
            }else{
                //全部用这种
                $html.="<script src=\"/admin/layuiadmin/layui/layui.js\"></script>\n<script>\n";
            }

            $html.="\tlayui.config({\n\t\tbase: '/admin/layuiadmin/' \n\t}).extend({\n\t\tindex: 'lib/index' \n\t}).use(['index', 'useradmin', 'form','upload','tinymce','layedit'], function(){\n\t\tvar $ = layui.$,form = layui.form,upload = layui.upload,form = layui.form,t = layui.tinymce;\n";
            
            /**上传图片**/
            if($isimg){
                if($pararr) {
                    for ($i=0;$i<count($pararr);$i++) {
                        $style=$pararr[$i]['style'];
                        $titlex=$pararr[$i]['title'];//字段名
                        if($style==3){ //上传图片
                            $html.="\t\tvar uploadInst = upload.render({\n\t\t\telem: '#".$titlex."btn'\n\t\t\t,url: '/admin/index/upload'\n\t\t\t,ext: 'jpg|png|gif'\n\t\t\t,done: function(res){\n\t\t\t\tif(res.code == 0){\n\t\t\t\t\t$(this.item).parent().prev(\"div\").children(\"input\").val(res.filename);\n\t\t\t\t\t$(this.item).parent().prev(\"div\").children(\"img\").attr(\"src\",res.img);\n\t\t\t\t\tlayer.msg(\"上传成功\");\n\t\t\t\t}\n\t\t\t\tlayer.msg(res.msg);\n\t\t\t}\n\t\t});\n";
                        }
                    }
                }
            }

            /**富文本**/
            if($iscontent){
                //富文本
                // var edit = t.render({
                //     elem: "#texter"
                //     , height: 400
                //     ,images_upload_url:'/admin/Index/upload'
                //     // ...
                //     // 其余配置可参考官方文档
                // },function(opt,edit){
                //     //加载完成后回调
                //     //编辑器的内容改变时执行 把值赋给textarea
                //     edit.on('blur', function(){
                //         $("#texter2").val(edit.getContent());
                //     });
                // });

                //添加以上代码
                $html.="\t\tvar edit = t.render({\n\t\t\telem: \"#".$fuid."\"\n\t\t\t, height: 400\n\t\t\t,images_upload_url:'/admin/Index/upload'\n\t\t\t,convert_urls:false\n\t\t\t// ...\n\t\t\t// 其余配置可参考官方文档\n\t\t},function(opt,edit){\n\t\t\t//加载完成后回调\n\t\t\t//编辑器的内容改变时执行 把值赋给textarea\n\t\t\tedit.on('blur', function(){\n\t\t\t\t$(\"#".$fuid."2\").val(edit.getContent());\n\t\t\t});\n\t\t});\n";
                
            }

            //如果有uid字段则添加上面注释的这一段代码
            if($isuid){
                $html.="\n\t\tvar active = {";
                $html.="\n\t\t\tchaxun:function(){";
                $html.="\n\t\t\t\tlayer.open({";
                $html.="\n\t\t\t\t\ttype: 2";
                $html.="\n\t\t\t\t\t,title: '搜索用户'";
                $html.="\n\t\t\t\t\t,content: '/admin/User/sousuo.html'";
                $html.="\n\t\t\t\t\t,maxmin: true";
                $html.="\n\t\t\t\t\t,area: ['700px', '450px']";
                $html.="\n\t\t\t\t\t,btn: ['确定', '取消']";
                $html.="\n\t\t\t\t\t,success: function(layero,index){";
                $html.="\n\t\t\t\t\t\tvar body = layui.layer.getChildFrame('body', index);";
                $html.="\n\t\t\t\t\t\tform.render();";
                $html.="\n\t\t\t\t\t}";
                $html.="\n\t\t\t\t\t,yes: function(index, layero){";
                $html.="\n\t\t\t\t\t\tvar iframeWindow = window['layui-layer-iframe'+ index]";
                $html.="\n\t\t\t\t\t\t\t,submitID = 'LAY-user-front-submit'";
                $html.="\n\t\t\t\t\t\t\t,submit = layero.find('iframe').contents().find('#'+ submitID);";
                $html.="\n\t\t\t\t\t\t//监听提交";
                $html.="\n\t\t\t\t\t\tiframeWindow.layui.form.on('submit('+ submitID +')', function(data){";
                $html.="\n\t\t\t\t\t\t\tvar field = data.field; //获取提交的字段";
                $html.="\n\t\t\t\t\t\t\tvar uid=field.uids;";
                $html.="\n\t\t\t\t\t\t\t$('#uid').val(uid);";
                $html.="\n\t\t\t\t\t\t\tlayer.close(index); //关闭弹层";
                $html.="\n\t\t\t\t\t\t});";
                $html.="\n\t\t\t\t\t\tsubmit.trigger('click');";
                $html.="\n\t\t\t\t\t}";
                $html.="\n\t\t\t\t});";
                $html.="\n\t\t\t}";
                $html.="\n\t\t}";
                $html.="\n\t\t$('.chaxun').on('click', function(){";
                $html.="\n\t\t\tvar type = $(this).data('type');";
                $html.="\n\t\t\tactive[type] ? active[type].call(this) : '';";
                $html.="\n\t\t});";
            }

            
                $html.="\t});\n";
            $html.="</script>\n</body>\n</html>";
            file_put_contents($path3,$html);
        }
    }

    //生成前端php代码
    public function apimodelaction($model,$action,$pararr,$title,$table,$daochu,$frontapi){
        
        //判断是否有 此文件 php文件
        $path=app()->getRootPath().'/app/api/controller/'.$model.'.php';
        if(!file_exists($path)){ //没有此文件 则生成
            $html="<?php \n/**\n*  * 系统-受国家计算机软件著作权保护 - !\n* =========================================================\n* Copy right 2018-2025 成都海之心科技有限公司, 保留所有权利。\n* ----------------------------------------------\n* 官方网址: http://www.ohyu.cn\n* 这不是一个自由软件！在未得到官方有效许可的前提下禁止对程序代码进行修改和使用。\n* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。\n* =========================================================\n* User: ohyueo\n* Date: ".(date('Y/m/d'))."\n* Time: ".(date('H:i'))."\n*/\nnamespace app\api\controller;\nuse app\BaseController;\nuse think\\facade\View;\nuse think\Request;\nuse think\\facade\Cache;\nuse think\\facade\Log;\nuse think\\facade\Db;\n";
            
            //引入use app\validate\api\yue;
            $html.="\nuse app\\validate\api\\v".strtolower($model).";\n";
            //判断app\validate\api\$model是否有这个文件夹  没有就生成
            $path2=app()->getRootPath().'/app/validate/api/v'.strtolower($model);
            if (!is_dir($path2)) {

                mkdir(iconv("UTF-8", "GBK", $path2), 0777, true);
            }

            //引入模型文件
            $modelname=$this->modelprefix.ucfirst($model).ucfirst($action);
            $html.="\nuse app\api\models\\".$modelname.";";
            $html.="\nclass ".$model." extends BaseController{\n";

            //在app\api\route\app.php里面添加路由
            $path3=app()->getRootPath().'/app/api/route/app.php';
            $html3="\nRoute::group(function(){\n";
                

        }else{
            $html='';
            $html3='';
            //引入模型文件
            $modelname=$this->modelprefix.ucfirst($model).ucfirst($action);

            //在app\api\route\app.php里面添加路由
            $path3=app()->getRootPath().'/app/api/route/app.php';
        }
        //循环$frontapi里面的值如果有并且==1则生成前端当前模型的列表
        if($frontapi){
            //判断$frontapi是否有1这个值
            $frontapiarr=$frontapi;
            if(in_array(1,$frontapiarr)){ //列表页
                // $title列表
                // $page=input('param.page/d',1);
                // $limit=input('param.limit/d',10);
                // $count=YuyueOrder::count();
                // $list=YuyueOrder::order('id desc')->page($page,$limit)->select();
                // $data = [
                //     'count' => $count,
                //     'list' => $list
                // ];
                // return $this->message('请求成功', $data);
                $html.="\n\t//".$title."列表";
                $html.="\n\tpublic function ".$action."(){";
                $html.="\n\t\t\$page=input('param.page/d',1);";
                $html.="\n\t\t\$limit=input('param.limit/d',10);";
                //$html.="\n\t\t\$count=".$modelname."::count();";
                //$html.="\n\t\t\$list=".$modelname."::order('id desc')->page(\$page,\$limit)->select();";

                //$count = AutoWorkerTexter::countData(['where'=>$where]);
                //$list=AutoWorkerTexter::findAllData(['where'=>$where,'limit'=>$limit,'page'=>$page,'order'=>'id Desc','field'=>'*']);

                $html.="\n\t\t\$count=".$modelname."::countData();";
                $html.="\n\t\t\$list=".$modelname."::findAllData(['limit'=>\$limit,'page'=>\$page,'order'=>'id Desc','field'=>'*']);";

                $html.="\n\t\t\$data = [";
                $html.="\n\t\t\t'count' => \$count,";
                $html.="\n\t\t\t'list' => \$list";
                $html.="\n\t\t];";
                $html.="\n\t\treturn \$this->message('请求成功', \$data);";
                $html.="\n\t}\n";

                //添加路由***
                $routname=$model.$action;
                //转换为小写
                $routname=strtolower($routname);
                //查询路由里面是否有当前列表页的路由
                $authfile=file_get_contents($path3);
                if(strpos($authfile,$routname)===false){
                    $html3.="\tRoute::post('".$routname."', '".$action."');\n";
                }
            }
            if(in_array(2,$frontapiarr)){ //详情页
                // $id=input('param.id/d',0);
                // $info=YuyueOrder::find(\$id);
                // return $this->message('请求成功', \$info);
                $html.="\n\t//".$title."详情";
                $html.="\n\tpublic function ".$action."info(){";
                $html.="\n\t\t\$id=input('param.id/d',0);";
                $html.="\n\t\t\$info=".$modelname."::findData(['where'=>['id'=>\$id]]);";
                $html.="\n\t\treturn \$this->message('请求成功', \$info);";
                $html.="\n\t}\n";

                //添加路由***
                $routname=$model.$action.'info';
                //转换为小写
                $routname=strtolower($routname);
                //查询路由里面是否有当前列表页的路由
                $authfile=file_get_contents($path3);
                if(strpos($authfile,$routname)===false){
                    $html3.="\tRoute::post('".$routname."', '".$action."info');\n";
                }
            }
            if(in_array(3,$frontapiarr)){ //添加
                
                //判断app\api\validate\{$action}\Add{$action}Validate.php是否存在
                $path2=app()->getRootPath().'/app/validate/api/v'.$model.'/Add'.$action.'Validate.php';
                if(!file_exists($path2)){
                    $html2="<?php\nnamespace app\\validate\api\\v".$model.";\nuse think\\Validate;\nclass Add".$action."Validate extends Validate{\n";
                    $html2.="\tprotected \$rule = [\n";
                    if($pararr) {
                        for ($i=0;$i<count($pararr);$i++) {
                            $name=$pararr[$i]['name'];
                            $titlex=$pararr[$i]['title'];//字段名
                            $style=$pararr[$i]['style'];
                            $val=$pararr[$i]['val'];//表单值
                            if($style==1){ //文本框
                                $html2.="\t\t'".$titlex."' => 'require',\n";
                            }else if($style==2){ //单选框
                                $html2.="\t\t'".$titlex."' => 'require',\n";
                            }else if($style==3){ //上传图片
                                $html2.="\t\t'".$titlex."' => 'require',\n";
                            }else if($style==4){ //富文本
                                $html2.="\t\t'".$titlex."' => 'require',\n";
                            }
                        }
                    }
                    $html2.="\t];\n";
                    $html2.="\tprotected \$message = [\n";
                    if($pararr) {
                        for ($i=0;$i<count($pararr);$i++) {
                            $name=$pararr[$i]['name'];
                            $titlex=$pararr[$i]['title'];//字段名
                            $style=$pararr[$i]['style'];
                            $val=$pararr[$i]['val'];//表单值
                            if($style==1){ //文本框
                                $html2.="\t\t'".$titlex.".require' => '".$name."不能为空',\n";
                            }else if($style==2){ //单选框
                                $html2.="\t\t'".$titlex.".require' => '".$name."不能为空',\n";
                            }else if($style==3){ //上传图片
                                $html2.="\t\t'".$titlex.".require' => '".$name."不能为空',\n";
                            }else if($style==4){ //富文本
                                $html2.="\t\t'".$titlex.".require' => '".$name."不能为空',\n";
                            }
                        }
                    }
                    $html2.="\t];\n";
                    $html2.="\tprotected \$scene = [\n";
                    $html2.="\t\t'add' => [\n";
                    if($pararr) {
                        for ($i=0;$i<count($pararr);$i++) {
                            $name=$pararr[$i]['name'];
                            $titlex=$pararr[$i]['title'];//字段名
                            $style=$pararr[$i]['style'];
                            $val=$pararr[$i]['val'];//表单值
                            $html2.="\t\t\t'".$titlex."',\n";
                        }
                    }
                    $html2.="\t\t],\n";
                    $html2.="\t];\n";
                    $html2.="}\n";
                    file_put_contents($path2,$html2);
                }


                // public function addYuyue(Request $request){
                //     // 获取当前时间  控制并发
                //     $time = time();
                //     // 生成一个基于当前时间的键
                //     $key = "visits:{$time}";
                //     // 获取键的值
                //     $visits = Cache::get($key, 0);
                //     if ($visits >= 2) { //并发 1秒2个
                //         // 如果键的值大于或等于2，就提示用户稍后再试
                //         return $this->message('系统繁忙，请稍后再试', [], 0);
                //     } else {
                //         // 如果键的值小于2，就将键的值加1，并设置键的过期时间为1秒
                //         Cache::set($key, $visits + 1, 1);
                //     }
                //     // 验证器
                //     $data = $request->post();
                //     // 检验数据
                //     validate(yue\AddYuyueValidate::class)->check($data);
                //     //添加事务
                //     $user = $this->user();
                //     if(!$user){
                //         return $this->message('重新登录', [], 201);
                //     }
                //     $uid=$user->id;
                //     if($data['uid']){
                //         $data['uid']=$uid;
                //     }
                //     if($data['addtime']){
                //         $data['addtime']=gettime();
                //     }
                //     //开启事务 
                //     Db::startTrans();
                //     try {
                //         $res=YuyueOrder::create($data);
                //         $rei=$res->id;
                //         $msg='添加成功';
                //         $rearr['id']=$reid;
                //         $rearr['money']=0;
                //         $code=200;
                //         // 提交事务
                //         Db::commit();
                //     } catch (\Exception $e) {
                //         // 回滚事务
                //         Db::rollback();
                //         $code=0;
                //         $error=$e->getMessage();
                //         $msg='添加失败'.$error;
                //         Log::info($msg);
                //         $rearr=[];
                //     }
                //     return $this->message($msg, $rearr, $code);
                // }

                //根据以上添加格式对应文中的模型和方法生成对应的添加代码
                $html.="\n\t//添加".$title;
                $html.="\n\tpublic function add".$action."(Request \$request){";
                $html.="\n\t\t// 获取当前时间  控制并发";
                $html.="\n\t\t\$time = time();";
                $html.="\n\t\t// 生成一个基于当前时间的键";
                $html.="\n\t\t\$key = \"visits:{\$time}\";";
                $html.="\n\t\t// 获取键的值";
                $html.="\n\t\t\$visits = Cache::get(\$key, 0);";
                $html.="\n\t\tif (\$visits >= 2) { //并发 1秒2个";
                $html.="\n\t\t\t// 如果键的值大于或等于2，就提示用户稍后再试";
                $html.="\n\t\t\treturn \$this->message('系统繁忙，请稍后再试', [], 0);";
                $html.="\n\t\t} else {";
                $html.="\n\t\t\t// 如果键的值小于2，就将键的值加1，并设置键的过期时间为1秒";
                $html.="\n\t\t\tCache::set(\$key, \$visits + 1, 1);";
                $html.="\n\t\t}";
                $html.="\n\t\t// 验证器";
                $html.="\n\t\t\$data = \$request->post();";
                $html.="\n\t\t// 检验数据";
                $html.="\n\t\tvalidate(v".strtolower($model)."\\Add".$action."Validate::class)->check(\$data);";
                $html.="\n\t\t//添加事务";
                $html.="\n\t\t\$user = \$this->user();";
                $html.="\n\t\tif(!\$user){";
                $html.="\n\t\t\treturn \$this->message('重新登录', [], 201);";
                $html.="\n\t\t}";
                $html.="\n\t\t\$uid=\$user->id;";
                    
                $html.="\n\t\t//重新定义data，并循环接受到的值";
                //重新定义data，并循环接受到的值
                $html.="\n\t\t\$data = [";
                if($pararr) {
                    for ($i=0;$i<count($pararr);$i++) {
                        $titlex=$pararr[$i]['title'];//字段名
                        $html.="\n\t\t\t'".$titlex."'=>\$data['".$titlex."'],";
                    }
                }
                $html.="\n\t\t];";


                $html.="\n\t\tif(\$uid){";
                $html.="\n\t\t\t\$data['uid']=\$uid;";
                $html.="\n\t\t}";
                $html.="\n\t\t\$data['addtime']=gettime();";
                $html.="\n\t\t//开启事务 ";
                $html.="\n\t\tDb::startTrans();";
                $html.="\n\t\ttry {";
                $html.="\n\t\t\t\$res=".$modelname."::addData(\$data);";
                $html.="\n\t\t\t\$reid=\$res->id;";
                $html.="\n\t\t\t\$msg='添加成功';";
                $html.="\n\t\t\t\$rearr['id']=\$reid;";
                $html.="\n\t\t\t\$rearr['money']=0;";
                $html.="\n\t\t\t\$code=200;";
                $html.="\n\t\t\t// 提交事务";
                $html.="\n\t\t\tDb::commit();";
                $html.="\n\t\t} catch (\\Exception \$e) {";
                $html.="\n\t\t\t// 回滚事务";
                $html.="\n\t\t\tDb::rollback();";
                $html.="\n\t\t\t\$code=0;";
                $html.="\n\t\t\t\$error=\$e->getMessage();";
                $html.="\n\t\t\t\$msg='添加失败'.\$error;";
                $html.="\n\t\t\tLog::info(\$msg);";
                $html.="\n\t\t\t\$rearr=[];";
                $html.="\n\t\t}";
                $html.="\n\t\treturn \$this->message(\$msg, \$rearr, \$code);";
                $html.="\n\t}\n";

                //添加路由***
                $routname=$model.'add'.$action;
                //转换为小写
                $routname=strtolower($routname);
                //查询路由里面是否有当前列表页的路由
                $authfile=file_get_contents($path3);
                if(strpos($authfile,$routname)===false){
                    $html3.="\tRoute::post('".$routname."', 'add".$action."');\n";
                }
                
            }
            if(in_array(4,$frontapiarr)){ //修改

                //判断app\api\validate\{$action}\Edit{$action}Validate.php是否存在
                $path2=app()->getRootPath().'/app/validate/api/v'.$model.'/Edit'.$action.'Validate.php';
                if(!file_exists($path2)){
                    $html2="<?php\nnamespace app\\validate\api\\v".$model.";\nuse think\\Validate;\nclass Edit".$action."Validate extends Validate{\n";
                    $html2.="\tprotected \$rule = [\n";
                    if($pararr) {
                        for ($i=0;$i<count($pararr);$i++) {
                            $name=$pararr[$i]['name'];
                            $titlex=$pararr[$i]['title'];//字段名
                            $style=$pararr[$i]['style'];
                            $val=$pararr[$i]['val'];//表单值
                            if($style==1){ //文本框
                                $html2.="\t\t'".$titlex."' => 'require',\n";
                            }else if($style==2){ //单选框
                                $html2.="\t\t'".$titlex."' => 'require',\n";
                            }else if($style==3){ //上传图片
                                $html2.="\t\t'".$titlex."' => 'require',\n";
                            }else if($style==4){ //富文本
                                $html2.="\t\t'".$titlex."' => 'require',\n";
                            }
                        }
                    }
                    $html2.="\t];\n";
                    $html2.="\tprotected \$message = [\n";
                    if($pararr) {
                        for ($i=0;$i<count($pararr);$i++) {
                            $name=$pararr[$i]['name'];
                            $titlex=$pararr[$i]['title'];//字段名
                            $style=$pararr[$i]['style'];
                            $val=$pararr[$i]['val'];//表单值
                            if($style==1){ //文本框
                                $html2.="\t\t'".$titlex.".require' => '".$name."不能为空',\n";
                            }else if($style==2){ //单选框
                                $html2.="\t\t'".$titlex.".require' => '".$name."不能为空',\n";
                            }else if($style==3){ //上传图片
                                $html2.="\t\t'".$titlex.".require' => '".$name."不能为空',\n";
                            }else if($style==4){ //富文本
                                $html2.="\t\t'".$titlex.".require' => '".$name."不能为空',\n";
                            }
                        }
                    }
                    $html2.="\t];\n";
                    $html2.="\tprotected \$scene = [\n";
                    $html2.="\t\t'add' => [\n";
                    if($pararr) {
                        for ($i=0;$i<count($pararr);$i++) {
                            $name=$pararr[$i]['name'];
                            $titlex=$pararr[$i]['title'];//字段名
                            $style=$pararr[$i]['style'];
                            $val=$pararr[$i]['val'];//表单值
                            $html2.="\t\t\t'".$titlex."',\n";
                        }
                    }
                    $html2.="\t\t],\n";
                    $html2.="\t];\n";
                    $html2.="}\n";
                    file_put_contents($path2,$html2);
                }

                // public function editYuyue(Request $request){
                //     // 验证器
                //     $data = $request->post();
                //     // 检验数据
                //     validate(yue\EditYuyueValidate::class)->check($data);
                //     //添加事务
                //     $user = $this->user();
                //     if(!$user){
                //         return $this->message('重新登录', [], 201);
                //     }
                //     $uid=$user->id;
                //     if($data['uid']){
                //         $data['uid']=$uid;
                //     }
                //自行添加修改条件
                //     //开启事务 
                //     Db::startTrans();
                //     try {
                //         $res=YuyueOrder::update($data);
                //         $msg='修改成功';
                //         $rearr['id']=$data['id'];
                //         $rearr['money']=0;
                //         $code=200;
                //         // 提交事务
                //         Db::commit();
                //     } catch (\Exception $e) {
                //         // 回滚事务
                //         Db::rollback();
                //         $code=0;
                //         $error=$e->getMessage();
                //         $msg='修改失败'.$error;
                //         Log::info($msg);
                //         $rearr=[];
                //     }
                //     return $this->message($msg, $rearr, $code);
                // }

                //根据以上添加格式对应文中的模型和方法生成对应的添加代码
                $html.="\n\t//修改".$title;
                $html.="\n\tpublic function edit".$action."(Request \$request){";
                $html.="\n\t\t// 验证器";
                $html.="\n\t\t\$data = \$request->post();";
                $html.="\n\t\t// 检验数据";
                $html.="\n\t\tvalidate(v".strtolower($model)."\\Edit".$action."Validate::class)->check(\$data);";
                $html.="\n\t\t//添加事务";
                $html.="\n\t\t\$user = \$this->user();";
                $html.="\n\t\tif(!\$user){";
                $html.="\n\t\t\treturn \$this->message('重新登录', [], 201);";
                $html.="\n\t\t}";
                $html.="\n\t\t\$uid=\$user->id;";
                $html.="\n\t\t\$id=\$data['id'];";

                $html.="\n\t\t//重新定义data，并循环接受到的值";
                //重新定义data，并循环接受到的值
                $html.="\n\t\t\$data = [";
                if($pararr) {
                    for ($i=0;$i<count($pararr);$i++) {
                        $titlex=$pararr[$i]['title'];//字段名
                        $html.="\n\t\t\t'".$titlex."'=>\$data['".$titlex."'],";
                    }
                }
                $html.="\n\t\t];";
                $html.="\n\t\t//自行添加修改条件";
                $html.="\n\t\t//开启事务 ";
                $html.="\n\t\tDb::startTrans();";
                $html.="\n\t\ttry {";
                $html.="\n\t\t\t\$res=".$modelname."::updatesData(['where'=>['id'=>\$id,'uid'=>\$uid],'data'=>\$data]);";
                $html.="\n\t\t\t\$msg='修改成功';";
                $html.="\n\t\t\t\$rearr['id']=\$data['id'];";
                $html.="\n\t\t\t\$rearr['money']=0;";
                $html.="\n\t\t\t\$code=200;";
                $html.="\n\t\t\t// 提交事务";
                $html.="\n\t\t\tDb::commit();";
                $html.="\n\t\t} catch (\\Exception \$e) {";
                $html.="\n\t\t\t// 回滚事务";
                $html.="\n\t\t\tDb::rollback();";
                $html.="\n\t\t\t\$code=0;";
                $html.="\n\t\t\t\$error=\$e->getMessage();";
                $html.="\n\t\t\t\$msg='修改失败'.\$error;";
                $html.="\n\t\t\tLog::info(\$msg);";
                $html.="\n\t\t\t\$rearr=[];";
                $html.="\n\t\t}";
                $html.="\n\t\treturn \$this->message(\$msg, \$rearr, \$code);";
                $html.="\n\t}\n";

                //添加路由***
                $routname=$model.'edit'.$action;
                //转换为小写
                $routname=strtolower($routname);
                //查询路由里面是否有当前列表页的路由
                $authfile=file_get_contents($path3);
                if(strpos($authfile,$routname)===false){
                    $html3.="\tRoute::post('".$routname."', 'edit".$action."');\n";
                }

            }
            if(in_array(5,$frontapiarr)){ //删除

                //根据以上添加格式对应文中的模型和方法生成对应的添加代码
                $html.="\n\t//删除".$title;
                $html.="\n\tpublic function del".$action."(){";
                $html.="\n\t\t\$id=input('param.id/d',0);";
                $html.="\n\t\t//自己增加删除条件";
                $html.="\n\t\t\$res=".$modelname."::deleteData(['where'=>['id'=>\$id]]);";
                $html.="\n\t\tif(\$res){";
                $html.="\n\t\t\treturn \$this->message('删除成功', [], 200);";
                $html.="\n\t\t}else{";
                $html.="\n\t\t\treturn \$this->message('删除失败', [], 0);";
                $html.="\n\t\t}";
                $html.="\n\t}\n";

                //添加路由***
                $routname=$model.'del'.$action;
                //转换为小写
                $routname=strtolower($routname);
                //查询路由里面是否有当前列表页的路由
                $authfile=file_get_contents($path3);
                if(strpos($authfile,$routname)===false){
                    $html3.="\tRoute::post('".$routname."', 'del".$action."');\n";
                }


            }

        }


       

        if(!file_exists($path)){ //没有此文件 则生成
            $html.="\n}";
            file_put_contents($path,$html);

            /***生成路由str****/
            $html3.="\n})->prefix('".$model."/');\n";
            //在文件末尾的第二行添加代码
            $handle = fopen($path3, 'r+');
            $i = -1;
            $lastLine = '';
            while(true){
                fseek($handle, $i, SEEK_END);
                $char = fgetc($handle);
                if($char == "\n"){
                    fwrite($handle, $html3. $lastLine);
                    break;
                }else{
                    $lastLine .= $char;
                }
                $i --;
            }
            /***生成路由end****/

        }else{
            //在文件末尾的第二行添加代码
            $handle = fopen($path, 'r+');
            $i = -1;
            $lastLine = '';
            while(true){
                fseek($handle, $i, SEEK_END);
                $char = fgetc($handle);
                if($char == "\n"){
                    fwrite($handle, $html. $lastLine);
                    break;
                }else{
                    $lastLine .= $char;
                }
                $i --;
            }


            /***生成路由str****/
            //查询这个路由在多少行 然后在这个路由的上一行添加代码
            //查找$action = [在文件中的行数
            $lineNumber = 0;
            $lines = file($path3);
            foreach ($lines as $lineNumber => $line) {
                if (strpos($line, "})->prefix('".$model."')") !== false) {
                    break;
                }
            }
            // 读取文件内容到数组中
            $files = file($path3);
            // 在特定的行插入新的内容
            array_splice($files, $lineNumber -1, 0, $html3);
            // 把数组的内容写回到文件中
            file_put_contents($path3, $files);

        }
    }

    public function test(){
        $arr=[
            0=>array('title'=>'name','type'=>1,'name'=>'姓名'),
            1=>array('title'=>'title','type'=>1,'name'=>'名称'),
            2=>array('title'=>'texter','type'=>3,'name'=>'内容'),
        ];
        $model='Offer';
        $action='material';
        $pararr=Db::name('ohyueo_develo_curdtable')->where('listid',1)->select()->toArray();
        if($pararr){
            for ($i=0;$i<count($pararr);$i++){
                $pararr[$i]['title']=$pararr[$i]['name'];
                $pararr[$i]['name']=$pararr[$i]['beizhu'];
            }
        }
        $title='商品订单';
        $table='escort_offer_material';
        //生成静态文件
        //$this->generatehtml($model,$action,$pararr,$title,$table);
        //echo "xxx";
        //$this->modelaction('Logins','custlist',$arr,'客户类别','ohyueo_develo_curdtable');
    }
}